◐ Shell
clean mode source ↗

Chapter 2: temp forecast using pyowm by amitsaha · Pull Request #6 · doingmathwithpython/code

@@ -1,24 +1,39 @@ ''' Retrieve a day's forecast of New York City and create a graph Retrieve three days' forecast of New York City and create a graph Using https://github.com/csparpa/pyowm ''' import matplotlib.pyplot as plt from pyowm import OWM import pytz
owm = OWM() owm = OWM('<token>')
def get_forecast(city): fc = owm.daily_forecast(city, limit=1) # https://github.com/csparpa/pyowm/issues/266 fc = owm.three_hours_forecast(city) f = fc.get_forecast() w = f.get_weathers()[0] forecast_temp = w.get_temperature('celsius') day_intervals = ['morn', 'day', 'eve', 'night']
# three_hours_forecast() returns 5 day forecast at a granularity # of three hours # we plot them all weathers = f.get_weathers()

data_points = ['temp', 'temp_max', 'temp_min', 'temp_kf'] temp = [] for timeofday in day_intervals: temp.append(forecast_temp[point]) x = range(1, len(day_intervals)+1) date_time = [] for w in weathers: forecast_temp = w.get_temperature('celsius') utc_dt = datetime.utcfromtimestamp(w.get_reference_time()).replace(tzinfo=pytz.utc) tz = pytz.timezone('America/New_York') dt = utc_dt.astimezone(tz)
# print it date_time.append(dt.strftime('%Y-%m-%d %H:%M:%S %Z%z')) temp.append(forecast_temp['temp']) x = range(1, len(temp)+1) plt.plot(x, temp, 'o-') plt.xticks(x, day_intervals) plt.xticks(x, date_time) plt.show()
if __name__ == '__main__': Expand Down