In [5]:
import urllib.request
import json
In [6]:
cities = {'Washington':'38.9,77.0',
'Atlantic City':'39.4,74.4',
'Philadelphia':'40.0,75.1',
'New York':'40.7,73.9',
'Nantucket':'41.3,70.1',
'New Haven':'41.3,72.9',
'New Bedford':'41.7,70.9',
'Cape Cod':'41.8,70.5',
'Boston': '42.4,71.1',
}
In [7]:
api_str = 'https://api.darksky.net/forecast/817a1054cf8cf001633f17ede8d4ea77/'
In [8]:
weather = {}
for city in cities:
weather[city] = json.loads(urllib.request.urlopen(api_str+cities[city]).read())
In [9]:
for city in weather:
print("{:<15s}: {:7.4f} ({}), {} m/s, {}°F ({}°F)".format(city,
weather[city]['currently']['precipIntensity'],
weather[city]['currently']['precipType'],
weather[city]['currently']['windSpeed'],
weather[city]['currently']['temperature'],
weather[city]['currently']['apparentTemperature'],
))
In [10]:
import pandas as pd
df_weather = pd.DataFrame({"city":[city for city in cities],
"lat":[ float(loc.split(',')[0]) for loc in cities.values() ],
"long":[ float(loc.split(',')[1]) for loc in cities.values() ],
"intensity":[weather[city]['currently']['precipIntensity'] for city in cities],
"type":[weather[city]['currently']['precipType'] for city in cities],
"wind":[weather[city]['currently']['windSpeed'] for city in cities],
"temp":[weather[city]['currently']['temperature'] for city in cities],
"feelslike":[weather[city]['currently']['apparentTemperature'] for city in cities],
})
In [11]:
df_weather
Out[11]:
In [12]:
import cartopy
import cartopy.crs as ccrs
import cartopy.io.shapereader as shpreader
import seaborn as sns
import matplotlib.pyplot as plt
In [13]:
cmap = sns.cubehelix_palette(start=2.8, rot=0.1, as_cmap=True)
In [14]:
fig = plt.figure(figsize=(24, 36))
ax = plt.axes([0, 0, 1, 1],
projection=ccrs.PlateCarree())
ax.set_extent([-80, -66.5, 35, 45], ccrs.PlateCarree())
#ax.background_patch.set_visible(True)
#ax.outline_patch.set_visible(True)
shapename = 'admin_1_states_provinces_lakes_shp'
states_shp = shpreader.natural_earth(resolution='110m',
category='cultural', name=shapename)
for state in shpreader.Reader(states_shp).geometries():
facecolor = [0.9375, 0.9375, 0.859375]
edgecolor = 'black'
ax.add_geometries([state], ccrs.PlateCarree(),
facecolor=facecolor, edgecolor=edgecolor, alpha=0.5
)
# plot temperatures
plt.scatter((df_weather.long * -1).values,
df_weather.lat.values,
c=df_weather.temp,
cmap=cmap,
s=800,
zorder=2,
transform=ccrs.PlateCarree())
coords = zip((df_weather.long * -1).values, df_weather.lat.values)
for xy,s in zip(coords, df_weather.temp):
plt.annotate(xy=xy,
s=s,
fontsize=36,
)
plt.savefig("Temperature in North-East states.png")
plt.show()
In [ ]: