You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# -*- coding: gbk -*-
import csv
from matplotlib import pyplot as plt, ticker
from datetime import datetime
from matplotlib.ticker import MultipleLocator
filename = 'changsha天气.csv'
with open(filename) as f:
reader = csv.reader(f)
header_row = next(reader)
#获取每天的最高气温数据
dates: [datetime] = []
higts, lows = [], []
for row in reader:
try:
current_date = datetime.strptime(row[0], '%Y-%m-%d')
higt = int(row[1])
low = int(row[2])
except ValueError:
print(current_date, '缺少信息')
else:
dates.append(current_date)
higts.append(higt)
lows.append(low)
#根据数据绘制图形,设置窗口大小
fig = plt.figure(dpi=150, figsize=(12, 8))
#plot 拆线图 alpha=(0.1 - 0.9)表示透明度0为全透明1为不透明
plt.plot(dates, higts, c='red', alpha=0.5)
plt.plot(dates, lows, c='blue', alpha=0.5)
#填充区间
plt.fill_between(dates, lows, higts, facecolor='black', alpha=0.1)
#设置图形格式
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.title('长沙2023全年气温折线图', fontsize=20, weight='bold', color='blue', fontstyle='italic', loc='center')
plt.xlabel('', fontsize=16)
#日期标签倾斜
fig.autofmt_xdate()
plt.ylabel("温度", fontsize=16, loc='top')
plt.xlabel("年-月", fontsize=16, loc='right')
#设置刻度标记的大小
plt.tick_params(axis='both', which='major',direction='in', pad=3,labelsize=16)
plt.savefig('长沙2023年气温折线图.png')
plt.show()