您当前的位置: 首页 > 

壹小俊

暂无认证

  • 0浏览

    0关注

    885博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

第五课--csv的使用

壹小俊 发布时间:2019-08-27 19:52:12 ,浏览量:0

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

# filename = 'sitka_weather_07-2014.csv'
# filename = 'sitka_weather_2014.csv'
filename = 'death_valley_2014.csv'
with open(filename) as f:
    # 分析csv文件头
    reader = csv.reader(f)
    header_row = next(reader)
    print(header_row)
    # enumerate获取每个元素的索引及其值
    # 打印头文件及其位置
    for index, column_header in enumerate(header_row):
        print(index, column_header)
    # 提取并读取数据
    highs = []
    lows = []
    dates = []
    # 可能出现错误,对错误做处理
    for row in reader:
        try:
            current_date = datetime.strptime(row[0], "%Y-%m-%d")
            high = int(row[1])
            low = int(row[3])
        except ValueError:
            print(current_date,'missign date')
        else:
            dates.append(current_date)
            highs.append(high)
            lows.append(low)
    # print(highs)
# 根据数据绘制图
fig = plt.figure(dpi=128, figsize=(10, 6))
plt.plot(dates, highs, c="red")
plt.plot(dates, lows, c="blue")
# plt.title('Daily high temperatures,July 2014', fontsize=21)
plt.title('Daily high temperatures - 2014', fontsize=21)
plt.xlabel('', fontsize=16)
fig.autofmt_xdate()  # 绘制倾斜的X轴标签
plt.ylabel('Temperature (F)', fontsize=16)
# 给图表区域着色
plt.fill_between(dates, highs, lows, facecolor='yellow',alpha=0.1)
plt.tick_params(axis='both', which='major', labelsize=16)

plt.show()
关注
打赏
1664335782
查看更多评论
立即登录/注册

微信扫码登录

0.0411s