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()
相关推荐
日更嵌入式的打工仔11 分钟前
Ethercat EOE笔记
网络·笔记·ethercat
股朋公式网14 分钟前
斩仙飞刀、 通达信飞刀 源码
python·算法
不吃橘子的橘猫14 分钟前
NVIDIA DLI 《Build a Deep Research Agent》学习笔记
开发语言·数据库·笔记·python·学习·算法·ai
算法与双吉汉堡18 分钟前
【短链接项目笔记】6 短链接跳转
java·开发语言·笔记·后端·springboot
学Linux的语莫19 分钟前
python的基础使用
开发语言·python
万粉变现经纪人29 分钟前
如何解决 pip install SSL 报错 ValueError: check_hostname requires server_hostname 问题
网络·python·网络协议·beautifulsoup·bug·ssl·pip
逻极30 分钟前
FastAPI + SQLAlchemy 现代API项目实战:从零到上手的Python MySQL开发指南
python·mysql·fastapi·异步·sqlalchemy
吃人陈乐游刘34 分钟前
06实战经验X-anylabelingAI自动标注数据集-本地实现-方法二(2025年12月)保姆级教程
python·miniforge·xanylabeling
玄同76534 分钟前
Python 正则表达式:LLM 噪声语料的精准清洗
人工智能·python·自然语言处理·正则表达式·nlp·知识图谱·rag
2401_8414956439 分钟前
【机器学习】BP神经网络
人工智能·python·神经网络·机器学习·梯度下降法·反向传播·前向传播