In [1]:
%matplotlib inline
import pandas as pd
import matplotlib.pyplot as plt
import csv
import numpy as np
names = ["date", "city", "state", "country", "shape", "duration_seconds", "duration_reported", "description", "report_date", "latitude", "longitude"]
ufo = pd.read_csv("/srv/nbgrader/data/ufo-scrubbed-geocoded-time-standardized.csv", names = names, parse_dates = ["date", "report_date"])
In [2]:
total_time = ufo.groupby(["state"])["duration_seconds"].sum()
total_sightings = ufo.groupby(["state"])["report_date"].count()
In [3]:
df = pd.DataFrame([total_time, total_sightings])
In [4]:
df
Out[4]:
In [5]:
df.rename({'report_date': 'total_sightings'}, inplace=True)
In [6]:
ufo_vals = df.transpose()
In [7]:
ufo_vals.describe()
Out[7]:
In [8]:
ufo_vals.loc["mi"]
Out[8]:
In [9]:
import cartopy
In [10]:
states = cartopy.io.shapereader.natural_earth(resolution='110m', category='cultural',
name='admin_1_states_provinces_lakes_shp')
In [11]:
reader =cartopy.io.shapereader.Reader(states)
In [12]:
all_states = list(reader.records())
In [13]:
all_states[0]
Out[13]:
In [14]:
_.geometry
Out[14]:
In [15]:
geometries = [state.geometry for state in all_states]
In [16]:
fig = plt.figure()
ax = fig.add_subplot(111, projection = cartopy.crs.LambertConformal())
ax.set_extent([-125, -66.5, 20, 50], cartopy.crs.Geodetic())
ax.add_geometries(geometries, crs=cartopy.crs.PlateCarree(), edgecolors='k', facecolors = 'k')
Out[16]:
In [17]:
import matplotlib.cm as cm
In [18]:
cm.viridis(0.4)
Out[18]:
In [19]:
import ipywidgets
In [20]:
@ipywidgets.interact(field = ["total_sightings", "duration_seconds"] )
def make_plot(field):
fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(111, projection = cartopy.crs.LambertConformal())
ax.set_extent([-125, -66.5, 20, 50], cartopy.crs.Geodetic())
colors = {'IL': 'k', 'CA': 'r'}
min_val = np.log10(ufo_vals[field].min())
max_val = np.log10(ufo_vals[field].max())
for state in all_states:
postal_code = state.attributes['postal'].lower()
n = np.log10(ufo_vals.loc[postal_code][field])
norm = (n - min_val)/(max_val - min_val)
color = cm.viridis(norm)
ax.add_geometries( [state.geometry], crs = cartopy.crs.PlateCarree(),
facecolors = [color])
In [21]:
mass = ufo[ ufo["state"] == "ma" ]
In [22]:
import numpy as np
In [23]:
fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(111, projection = cartopy.crs.LambertConformal())
ax.set_extent([-75, -70, 40, 50], cartopy.crs.Geodetic())
ax.add_geometries(geometries, crs = cartopy.crs.PlateCarree(), zorder=0)
ax.scatter(mass["longitude"], mass["latitude"], transform = cartopy.crs.Geodetic(),
c=np.log10(mass["duration_seconds"]))
Out[23]:
In [24]:
import cartopy.io.img_tiles as cimgt
In [25]:
fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(111, projection = cartopy.crs.GOOGLE_MERCATOR)
ax.set_extent([-75, -70, 40, 50], cartopy.crs.Geodetic())
ax.add_image(cimgt.GoogleTiles(), 5)
ax.scatter(mass["longitude"], mass["latitude"], transform = cartopy.crs.Geodetic(),
c=np.log10(mass["duration_seconds"]))
Out[25]:
In [26]:
fig = plt.figure()
ax = fig.add_subplot(111, projection = cartopy.crs.Mollweide())
ax.coastlines()
Out[26]: