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()
相关推荐
董建光d1 天前
PyTorch 实现 MNIST 手写数字识别完整流程(含数据处理、模型构建与训练可视化)
人工智能·pytorch·python
hour_go1 天前
页表 vs. 组相联缓存:内存管理与性能优化的殊途同归
笔记·操作系统·分页·计算机体系结构·tlb·组相联缓存
小宁爱Python1 天前
从零搭建 RAG 智能问答系统3:聊天信息持久化和登录注册
python
天才少女爱迪生1 天前
LLVM(Low Level Virtual Machine)介绍
python·数据挖掘
superlls1 天前
(微服务)Dubbo 服务调用
笔记·rpc·dubbo
碳酸的唐1 天前
A* 工程实践全指南:从启发式设计到可视化与性能优化
python·神经网络
于小汐在咯1 天前
词根学习笔记 | Agri系列
笔记·学习
摇滚侠1 天前
Spring Boot 3零基础教程,Spring Intializer,笔记05
spring boot·笔记·spring
聪明的笨猪猪1 天前
Java Redis “高可用 — 主从复制”面试清单(含超通俗生活案例与深度理解)
java·经验分享·笔记·面试