Python项目:数据可视化

Python项目:数据可视化

码农世界 2024-05-31 前端 93 次浏览 0个评论

源自《Python编程:从入门到实践》

作者: Eric Matthes

02 下载数据

2.1 sitka_weather_07-2021_simple.csv

from pathlib import Path
import matplotlib.pyplot as plt
import csv
from datetime import datetime
path = Path('D:\CH16\sitka_weather_07-2021_simple.csv')
lines = path.read_text().splitlines()
reader = csv.reader(lines)
header_row = next(reader)
print(header_row)
#提取最高温度
#提取日期
highs = []
dates = []
for row in reader:
    high = int(row[4])
    highs.append(high)
    current_date = datetime.strptime(row[2], '%Y-%m-%d')
    dates.append(current_date)
print(highs)
print(dates)
#绘制温度图
plt.style.use('seaborn-v0_8')
fig, ax = plt.subplots()
ax.plot(dates, highs, color='red')
ax.set_title('Daily High Temperatures,July 2021', fontsize=24)
ax.set_xlabel('Date', fontsize=14)
ax.set_ylabel('Temperature(F)', fontsize=14)
plt.show()

2.2 sitka_weather_2021_simple.csv

from pathlib import Path
import matplotlib.pyplot as plt
import csv
from datetime import datetime
path = Path('D:\CH16\sitka_weather_2021_simple.csv')
lines = path.read_text().splitlines()
reader = csv.reader(lines)
header_row = next(reader)
print(header_row)
#提取最高温度
#提取日期
highs = []
dates = []
for row in reader:
    high = int(row[4])
    highs.append(high)
    current_date = datetime.strptime(row[2], '%Y-%m-%d')
    dates.append(current_date)
print(highs)
print(dates)
#绘制温度图
plt.style.use('seaborn-v0_8')
fig, ax = plt.subplots()
ax.plot(dates, highs, color='red')
ax.set_title('Daily High Temperatures,2021', fontsize=24)
ax.set_xlabel('', fontsize=14)
ax.set_ylabel('Temperature(F)', fontsize=14)
plt.show()

转载请注明来自码农世界,本文标题:《Python项目:数据可视化》

百度分享代码,如果开启HTTPS请参考李洋个人博客
每一天,每一秒,你所做的决定都会改变你的人生!

发表评论

快捷回复:

评论列表 (暂无评论,93人围观)参与讨论

还没有评论,来说两句吧...

Top