数据可视化——天气变化图

import csv
import matplotlib.pyplot as plt
from datetime import datetime


# 从文件中获取最高气温 最低气温
filename = 'sitka_weather_2014.csv'
with open(filename) as f:
    reader = csv.reader(f)
    header_row = next(reader) # next()返回文件的下一行,只调用了next()一次,则得到的是文件的第一行

    for index, column_header in enumerate(header_row):
        print(index, column_header)

    dates, highs, lows = [], [], []
    for row in reader:
        try:
            # 错误预知,避免出现有些部分没有数据
            current_date = datetime.strptime(row[0], '%Y-%m-%d')  # 使用strptime()将字符串日期转化为标准显示日期
            high = int(row[1])
            low = int(row[3])
        except ValueError:
            print(current_date, 'missing date')
        else:
            highs.append(int(row[1]))
            lows.append(int(row[3]))
            dates.append(current_date)

fig = plt.figure(dpi=128, figsize=(10, 6))
plt.plot(dates, highs, c='red', alpha=0.5) # alpha指定颜色透明度
plt.plot(dates, lows, c='blue', alpha=0.5) # 注意dates和highs 以及lows是匹配对应的
plt.fill_between(dates, highs, lows, facecolor='blue', alpha=0.1) # facecolor指定了区域的颜色

# 设置图形格式
plt.title("Daily high and low temperature, 2014", fontsize=24)
plt.xlabel('', fontsize=14)
fig.autofmt_xdate() # 让x轴标签斜着打印避免拥挤
plt.ylabel('Temperature(F)', fontsize=14)
plt.tick_params(axis='both', which='major', labelsize=14)


plt.show()

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值