气温图绘制

#coding:utf-8
import csv
from matplotlib import pyplot as plt
from datetime import datetime
# ~ first_date = datetime.strptime('2014-7-1', '%Y-%m-%d') #打印日期
# ~ print(first_date)
filename = 'sitka_weather_07-2014.csv'
with open(filename) as f:   #使用with 会在不用文件时,自动关闭文件
    dates, highs, lows = [], [], []
    reader = csv.reader(f)  #创建一个新的reader对象
    header_row = next(reader) #返回文件的下一行
    # ~ print(header_row)
    # ~ header_row = next(reader) #返回文件的下一行
    # ~ print(header_row)
    # ~ for index, column_header in enumerate(header_row): #获取索性及其值
        # ~ print(index, column_header)
    for row in reader: #获取第一列数据
        #获取最高温度
        high = int(row[1]) 
        highs.append(high)
        
        #获取最低气温
        low = int(row[3])
        lows.append(low)
        #获取日期
        current_date = datetime.strptime(row[0], "%Y-%m-%d")
        dates.append(current_date)
        
        
    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.4) #在区间填充颜色
    #设置图形的格式
    plt.ylim([45, 72]) #设置y坐标的取值范围
    plt.title("Daily high and low temperatures-July 2014", fontsize=24)
    plt.xlabel('', fontsize=16)
    plt.ylabel("Temperature (F)", fontsize=16) #给y轴加标签
    fig.autofmt_xdate() #使x轴标签倾斜
    plt.tick_params(axis='both', which='major', labelsize=16) #修改x轴和y轴的刻度
    
    plt.show()

需要导入一个csv文件

评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值