最近上了python这门课老师叫我们组队完成一个爬虫项目我和我的伙伴对天气预报进行了爬取,其中我负责对爬到的天气可视化的部分。下面是一些代码:
未来十四天高低温变化曲线图
可以看出高温平均在10.8度左右,低温在4.3度左右而且可以看出高低温都变化在第四条后变化都比较大。注意防止感冒
def tem_curve14(data,city_name):
date = list(data['日期'])
tem_low = list(data['最低气温'])
tem_high = list(data['最高气温'])
for i in range(0, 14):
if math.isnan(tem_low[i]):
tem_low[i] = tem_low[i - 1]
if math.isnan(tem_high[i]):
tem_high[i] = tem_high[i - 1]
tem_high_ave = sum(tem_high) / 14 # 求平均高温
tem_low_ave = sum(tem_low) / 14 # 求平均低温
tem_max = max(tem_high)
tem_max_date = tem_high.index(tem_max) # 求最高温度
tem_min = min(tem_low)
tem_min_date = tem_low.index(tem_min) # 求最低温度
x = range(1, 15)
plt.figure(1)
plt.plot(x, tem_high, color='red', label='高温') # 画出高温度曲线
plt.scatter(x, tem_high, color='red') # 点出每个时刻的温度点
plt.plot(x, tem_low, color='blue', label='低温') # 画出低温度曲线
plt.scatter(x, tem_low, color='blue') # 点出每个时刻的温度点
plt.plot([1, 15], [tem_high_ave, tem_high_ave], c='black', linestyle='--') # 画出平均温度虚线
plt.plot([1, 15], [tem_low_ave, tem_low_ave], c='black', linestyle='--') # 画出平均温度虚线
plt.legend()
plt.text(tem_max_date + 0.15, tem_max + 0.15, str(tem_max), ha='center', va='bottom', fontsize=10.5) # 标出最高温度
plt.text(tem_min_date + 0.15, tem_min + 0.15, str(tem_min), ha='center', va='bottom', fontsize=10.5) # 标出最低温度
plt.xticks(x)#输出图形,防止不显示
plt.title('未来14天高温低温变化曲线图')
plt.xlabel('未来天数/天')
plt.ylabel('摄氏度/℃')
plt.savefig('image/{}15天温度曲线.png'.format(city_name), bbox_inches='tight')
plt.show()