Skip to content

Commit c061d8d

Browse files
authored
Chapter 2: temp forecast using pyowm
1 parent d172ee1 commit c061d8d

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

chapter2/solutions/nyc_forecast_owm.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,32 @@
11
'''
2-
Retrieve a day's forecast of New York City and create a graph
2+
Retrieve three days' forecast of New York City and create a graph
33
Using https://github.com/csparpa/pyowm
44
'''
55
import matplotlib.pyplot as plt
66
from pyowm import OWM
77

8-
owm = OWM()
8+
owm = OWM('54c5451fcc146ce043f4f44153e4ea4b')
99

1010
def get_forecast(city):
11-
fc = owm.daily_forecast(city, limit=1)
11+
# https://github.com/csparpa/pyowm/issues/266
12+
fc = owm.three_hours_forecast(city)
1213
f = fc.get_forecast()
13-
w = f.get_weathers()[0]
14-
forecast_temp = w.get_temperature('celsius')
15-
day_intervals = ['morn', 'day', 'eve', 'night']
14+
15+
# three_hours_forecast() returns 5 day forecast at a granularity
16+
# of three hours
17+
# we plot them all
18+
weathers = f.get_weathers()
19+
20+
21+
data_points = ['temp', 'temp_max', 'temp_min', 'temp_kf']
1622
temp = []
17-
for timeofday in day_intervals:
18-
temp.append(forecast_temp[point])
19-
x = range(1, len(day_intervals)+1)
23+
for w in weathers:
24+
forecast_temp = w.get_temperature('celsius')
25+
#for point in data_points:
26+
temp.append(forecast_temp['temp'])
27+
x = range(1, len(temp)+1)
2028
plt.plot(x, temp, 'o-')
21-
plt.xticks(x, day_intervals)
29+
#plt.xticks(x, day_intervals)
2230
plt.show()
2331

2432
if __name__ == '__main__':

0 commit comments

Comments
 (0)