Nav apraksta

highs_lows.py 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import csv
  2. from datetime import datetime
  3. from matplotlib import pyplot as plt
  4. # Get dates, high, and low temperatures from file.
  5. filename = 'death_valley_2014.csv'
  6. with open(filename) as f:
  7. reader = csv.reader(f)
  8. header_row = next(reader)
  9. dates, highs, lows = [], [], []
  10. for row in reader:
  11. try:
  12. current_date = datetime.strptime(row[0], "%Y-%m-%d")
  13. high = int(row[1])
  14. low = int(row[3])
  15. except ValueError:
  16. print(current_date, 'missing data')
  17. else:
  18. dates.append(current_date)
  19. highs.append(high)
  20. lows.append(low)
  21. # Plot data.
  22. fig = plt.figure(dpi=128, figsize=(10, 6))
  23. plt.plot(dates, highs, c='red', alpha=0.5)
  24. plt.plot(dates, lows, c='blue', alpha=0.5)
  25. plt.fill_between(dates, highs, lows, facecolor='blue', alpha=0.1)
  26. # Format plot.
  27. title = "Daily high and low temperatures - 2014\nDeath Valley, CA"
  28. plt.title(title, fontsize=20)
  29. plt.xlabel('', fontsize=16)
  30. fig.autofmt_xdate()
  31. plt.ylabel("Temperature (F)", fontsize=16)
  32. plt.tick_params(axis='both', which='major', labelsize=16)
  33. plt.show()