当前位置:   article > 正文

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

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

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

作者: Eric Matthes

02 下载数据

2.1 sitka_weather_07-2021_simple.csv

  1. from pathlib import Path
  2. import matplotlib.pyplot as plt
  3. import csv
  4. from datetime import datetime
  5. path = Path('D:\CH16\sitka_weather_07-2021_simple.csv')
  6. lines = path.read_text().splitlines()
  7. reader = csv.reader(lines)
  8. header_row = next(reader)
  9. print(header_row)
  10. #提取最高温度
  11. #提取日期
  12. highs = []
  13. dates = []
  14. for row in reader:
  15. high = int(row[4])
  16. highs.append(high)
  17. current_date = datetime.strptime(row[2], '%Y-%m-%d')
  18. dates.append(current_date)
  19. print(highs)
  20. print(dates)
  21. #绘制温度图
  22. plt.style.use('seaborn-v0_8')
  23. fig, ax = plt.subplots()
  24. ax.plot(dates, highs, color='red')
  25. ax.set_title('Daily High Temperatures,July 2021', fontsize=24)
  26. ax.set_xlabel('Date', fontsize=14)
  27. ax.set_ylabel('Temperature(F)', fontsize=14)
  28. plt.show()

2.2 sitka_weather_2021_simple.csv

  1. from pathlib import Path
  2. import matplotlib.pyplot as plt
  3. import csv
  4. from datetime import datetime
  5. path = Path('D:\CH16\sitka_weather_2021_simple.csv')
  6. lines = path.read_text().splitlines()
  7. reader = csv.reader(lines)
  8. header_row = next(reader)
  9. print(header_row)
  10. #提取最高温度
  11. #提取日期
  12. highs = []
  13. dates = []
  14. for row in reader:
  15. high = int(row[4])
  16. highs.append(high)
  17. current_date = datetime.strptime(row[2], '%Y-%m-%d')
  18. dates.append(current_date)
  19. print(highs)
  20. print(dates)
  21. #绘制温度图
  22. plt.style.use('seaborn-v0_8')
  23. fig, ax = plt.subplots()
  24. ax.plot(dates, highs, color='red')
  25. ax.set_title('Daily High Temperatures,2021', fontsize=24)
  26. ax.set_xlabel('', fontsize=14)
  27. ax.set_ylabel('Temperature(F)', fontsize=14)
  28. plt.show()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/643941
推荐阅读
相关标签
  

闽ICP备14008679号