python从文件中读取每日最高气温代码添加了日期显示的代码 python入门到实践

highs_lows.py
import csv
from datetime import datetime

from matplotlib import pyplot as plt

#从文件中获取日期和最高气温
filename="sitka_weather_07-2014.csv"
with open(filename) as f:
    reader=csv.reader(f)
    header_row=next(reader)

    dates,highs=[],[]
    for row in reader:
        current_date=datetime.strptime(row[0],"%Y-%m-%d")
        dates.append(current_date)
        high=int(row[1])
        highs.append(high)
    print(highs)

#根据数据绘制图形
fig=plt.figure(dpi=128,figsize=(10,6))
plt.plot(dates,highs,c="red")
#设置图形的格式
plt.title("Daily high temperatures,July 2014",fontsize=24)
plt.xlabel("",fontsize=16)
fig.autofmt_xdate()
plt.ylabel("Temperature(F)",fontsize=16)
plt.tick_params(axis="both",which="major",labelsize=16)

plt.show()

以上就是python从文件中读取每日最高气温代码添加了日期显示的代码 python入门到实践的全部内容,供大家参考。如果觉的这篇文章对你
有帮助,欢迎收藏点赞转发,你也可以在评论区留言,就这个问题进行讨论!
(0)
陈玉龙的头像陈玉龙
上一篇 2020年7月28日
下一篇 2020年7月28日

相关推荐