1.讀取文件; 2.獲得想要的數據內容:日期、最低溫、最高溫 3.繪制圖形。 x軸:日期; y軸:溫度變化。
def read_weather_file():
"""讀取文件,提取內容"""
files = open("./weather/weather.json","r",encoding="utf-8")
weather_content = files.read()
# print("天氣信息:",weather_content)
# print(type(weather_content))
files.close()
#把json數據轉換成python類型
py_datas = json.loads(weather_content)
# print(py_data)
# print(type(py_data))
# 日期
date_datas = []
# 最低溫
tem_down_datas = []
# 最高溫
tem_up_datas = []
# 遍歷
for element in py_datas:
# print(element)
date = element["name"]
# print(date)
date_datas.append(date)
tem_down_datas.append(int(element["tem_low"][:-1]))
tem_up_datas.append(int(element["tem_up"]))
# print(date_datas)
# print(tem_down_datas)
# print(tem_up_datas)
# print(type(tem_up_datas))
return date_datas,tem_up_datas,tem_down_datas
# 繪圖
# 顯示中文
pyplot.rcParams["font.sans-serif"] = ["SimHei"]
# X軸
x = [i for i in range(len(datas))]
print(x)
# 繪制
pyplot.plot(x,tem_down,label="最低溫")
pyplot.plot(x,tem_up,label="最高溫")
# 限制Y軸的最大最小值
pyplot.ylim(20,40)
# Y刻度
y_tick = [i for i in range(20,40)]
pyplot.yticks(y_tick)
# X軸
pyplot.xticks(x,datas,rotation=45)
# 圖例
pyplot.legend(loc="lower left")
# 網格
pyplot.grid(alpha=0.4)
# 標題
pyplot.title("湛江一周天氣預報最高最低溫情況圖")
pyplot.ylabel("溫度(單位:℃)")
pyplot.xlabel("時間日期")
pyplot.show()
?
?
?
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
