Python项目:数据可视化_下载数据【笔记】

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

作者: Eric Matthes

02 下载数据

2.1 sitka_weather_07-2021_simple.csv

python 复制代码
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

python 复制代码
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()
相关推荐
xurileidian1 分钟前
第 22 篇 · 边玩边学Python · 双等号与 is 不是一回事
开发语言·python·spring
ever_you_love1 小时前
12.1课堂笔记
笔记
钱栈up2 小时前
GitHub热榜实测|Sherlock:一个用户名搜遍全网
python·开源·github
geovindu2 小时前
python: Gradient Boosting
开发语言·后端·python·算法·boosting
卿卿的产品经理日记2 小时前
【AI产品经理实战】Day 48|AI 回答变短了,到底是谁的功劳?
人工智能·笔记·aigc·产品经理
Agent世界2 小时前
发布台账字段漂移:1265 条记录里 190 条没有标题
python·自动化运维
用户298698530144 小时前
Python 自动化处理 Word:按规则查找并高亮关键文本
python·api·编程语言
人工智能技术咨询.5 小时前
什么才算高质量数据集
python
DanCheng-studio5 小时前
毕业设计项目 机器视觉指纹识别特征对比算法(源码+论文)
python·毕业设计·毕设
梅雅达编程笔记5 小时前
一份 PDF 到底该拆成什么存进去:RAG 智能问答毕设数据库设计完整教程
python·flask·智能问答·毕设·向量数据库·rag·数据库设计