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()
相关推荐
keke.shengfengpolang1 分钟前
2026秋招信用风险分析岗面试:SQL和Python到底要学到什么程度才能过?
python
噜~噜~噜~16 分钟前
操作系统笔记-3.1.1.3 进程的内存映像
笔记·操作系统
2601_9620990820 分钟前
python flask sqlalchemy连接数据库流程介绍
数据库·python·flask·教程·sqlalchemy
2601_9622932422 分钟前
python接口自动化(二十八)--html测试 报告——下(详解)
python·接口自动化·编码问题·htmltestrunner·报告优化
烂蜻蜓26 分钟前
Flask入门教程(附录):模板渲染API——Jinja2集成全解析
后端·python·flask
摇滚侠26 分钟前
《Spring Boot 3:高级与架构设计》第 2 章 IOC容器的高级机制 Environment 阅读笔记 4
spring boot·笔记·后端
2601_9623878230 分钟前
Python CUDA 编程 - 2 - Numba 简介
python·numpy·cuda·jit编译器·numba
智能体与具身智能9 小时前
TVA具身智能的概念、架构与应用(19)
人工智能·python·具身智能
李帅朋10 小时前
微分基本定义笔记
人工智能·笔记·深度学习·神经网络
2601_9622946110 小时前
python中range函数怎么用
python·for循环·可迭代对象·range函数·整数列表