import csv from datetime import datetime import matplotlib.pyplot as plt import matplotlib as mpl #解决画图乱码 mpl.rcParams['font.sans-serif']=['SimHei'] #定义文件路径 filename='./temperature.csv' #打开文件 with open(filename)as f: #读取文件内容 reader=csv.reader(f) #去掉表头 header_row=next(reader) print(header_row) #定义数组,存日期,最高温度,最低温度 dates,highs,lows=[],[],[] #遍历每行数据 for row in reader: try: current_date=datetime.strptime(row[0],'%Y-%m-%d') high=int(row[1]) low=int(row[3]) except ValueError: print('missing date') else: dates.append(current_date) highs.append(high) lows.append(low) #画图 fig = plt.figure(dpi=90,figsize=(10,6))#设置图的大小 plt.plot(dates,highs,c='red',alpha=0.5)#alpha设置透明度 plt.plot(dates,lows,c='blue',alpha=0.5) plt.title('2014年7月温度曲线',fontsize=24) fig.autofmt_xdate()#x,y轴字体变为斜体 plt.ylabel('制度',fontsize=16) plt.xlabel('日期',fontsize=16) plt.savefig('highs_lows.png') plt.show()
python画图
最新推荐文章于 2023-12-13 09:35:23 发布