代碼實現:
import csv
from matplotlib import pyplot as plt
from datetime import datetime
date=[]
airmax=[]
airmin=[]
with open(r'C:\Users\Administrator\Desktop\新建文本文檔 (3).csv') as cs:
read=csv.reader(cs)
header=next(read)
for i in read:
date.append(datetime.strptime(i[3],'%Y/%m/%d'))#datetime.strptime(),轉換成日期格式,第一個參數是所需的日期字符串,第二參數是告訴python如何設置字符串
airmax.append(int(i[0]))
airmin.append(int(i[1]))
print(airmax)
print(date)
fig=plt.figure(dpi=80,figsize=(10,6))
plt.plot(date,airmax,c='red')
plt.plot(date,airmin)
fig.autofmt_xdate() #設置x軸日期自動適應占位,防止重疊
plt.fill_between(date,airmax,airmin,facecolor='yellow',alpha=0.1)#填充最大最小值區間的范圍,facecolor顏色,alpha色度深淺
plt.show()