python讀取csv文件繪制氣溫圖,x軸為日期,並填充顏色


 
代碼實現:
 
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()
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM