-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmatplotlib_csv_example.py
99 lines (81 loc) · 2.69 KB
/
matplotlib_csv_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
'''Matplotlib – Weather Data'''
# The weather data for this example was originally obtained from:
# https://www.wunderground.com/history/
# Some other good data sources for historical weather data:
# https://www.ncdc.noaa.gov/data-access/quick-links
# http://climate.weather.gc.ca/index_e.html
import csv
from datetime import datetime
from matplotlib import pyplot as plt
filename = 'data/death_valley_2014.csv'
placename = 'Death Valley, CA'
# Exploring the data:
# -----------------------------------------------------------------------------
with open(filename) as fob:
reader = csv.reader(fob)
header_row = next(reader)
for index, column_header in enumerate(header_row):
print(index, column_header)
# 0 PST
# 1 Max TemperatureF
# 2 Mean TemperatureF
# 3 Min TemperatureF
# 4 Max Dew PointF
# 5 MeanDew PointF
# 6 Min DewpointF
# 7 Max Humidity
# 8 Mean Humidity
# 9 Min Humidity
# 10 Max Sea Level PressureIn
# 11 Mean Sea Level PressureIn
# 12 Min Sea Level PressureIn
# 13 Max VisibilityMiles
# 14 Mean VisibilityMiles
# 15 Min VisibilityMiles
# 16 Max Wind SpeedMPH
# 17 Mean Wind SpeedMPH
# 18 Max Gust SpeedMPH
# 19 PrecipitationIn
# 20 CloudCover
# 21 Events
# 22 WindDirDegrees
# Extracting and reading data:
# -----------------------------------------------------------------------------
with open(filename) as fob:
reader = csv.reader(fob)
header_row = next(reader)
dates, highs, lows = [], [], []
for row in reader:
try:
current_date = datetime.strptime(row[0], '%Y-%m-%d')
high = int(row[1])
low = int(row[3])
except ValueError:
print(current_date, 'missing data')
else:
dates.append(current_date)
highs.append(high)
lows.append(low)
# Plotting the data:
# -----------------------------------------------------------------------------
# creates a figure and one subplot
fig, ax = plt.subplots(figsize=(10, 5))
# plot the data:
plt.plot(dates, highs, c='tomato', alpha=0.6)
plt.plot(dates, lows, c='darkturquoise', alpha=0.6)
# format the plot:
plt.fill_between(dates, highs, lows, facecolor='papayawhip', alpha=0.6)
title = 'Daily high and low temperatures, 2014\n{}'.format(placename)
plt.title(title, fontsize=10)
plt.xlabel('', fontsize=9)
fig.autofmt_xdate()
plt.xticks(rotation=25)
plt.ylabel('Termperature (F)', fontsize=9, fontweight='bold')
plt.tick_params(axis='both', which='major', labelsize=7)
# tells x, y axis to use this range
# plt.axis([datetime(2014, 1, 1), datetime(2014, 12, 1), 0, 120])
# if you just want to set one axis:
ax.set_xlim([datetime(2014, 1, 1), datetime(2014, 12, 1)])
# ax.set_ylim([20, 120])
# display the plot:
plt.show()