实例3·加州死亡谷温度曲线(csv文件处理,图例字体倾斜)

    xiaoxiao2025-06-01  90

    import csv from datetime import datetime from matplotlib import pyplot as plt with open('C://Users/Administrator/Desktop/death_valley_2014.csv') as f: text = csv.reader(f) #表头 header_row = next(text) #获取时间,最高气温,最低气温 dates, highs, lows = [], [], [] for row in text: try: current_date = datetime.strptime(row[0], "%Y-%m-%d")#时间格式化输出 high = int(row[1])#第二列数据 low = int(row[3]) except ValueError: print(current_date, 'missing data') else: dates.append(current_date) highs.append(high) lows.append(low) #窗口设置 fig = plt.figure(dpi=128, figsize=(10, 6)) plt.plot(dates, highs, c='red', alpha=0.5)#最高温度曲线 plt.plot(dates, lows, c='blue', alpha=0.5)#最低温度曲线 plt.fill_between(dates, highs, lows, facecolor='blue', alpha=0.1)#颜色填充 # 图例设置 title = "日最高最低温度 - 2014年\n加州死亡谷" plt.title(title, fontproperties='SimHei',fontsize=20) plt.xlabel('时间', fontsize=16,fontproperties='SimHei') fig.autofmt_xdate()#生成倾斜字体,防止重叠 plt.ylabel("华氏度", fontsize=20, fontproperties='SimHei') plt.tick_params(axis='both', which='major', labelsize=16)#刻度标记设置 plt.savefig('C://Users/administrator/Desktop/F.png',dpi=800) plt.show()

    结果展示

    最新回复(0)