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: d = datetime.strptime(row[0], '%Y-%m-%d') h = int(row[1]) l = int(row[3]) except ValueError: print('missing value') else: dates.append(d) highs.append(h) lows.append(l) # 画图 fig = plt.figure(dpi=100, figsize=(10, 6)) plt.plot(dates, highs, c='red', alpha=0.5) plt.plot(dates, lows, c='blue', alpha=0.3) plt.title("2014年7月温度曲线", fontsize=24) fig.autofmt_xdate()# 设置x轴温度斜体 plt.xlabel("日期", fontsize=16) plt.ylabel("温度", fontsize=16) plt.savefig('temperature.png') plt.show()
matplotlib画图
最新推荐文章于 2024-02-16 14:27:57 发布