<jupyter_start><jupyter_text>Basic maps with cartopy<jupyter_code># import our usual things
%matplotlib inline
import cartopy
import pandas as pd
import matplotlib.pyplot as plt
# make plots a little bigger
plt.rcParams['figure.dpi'] = 100 # 300
states_files = cartopy.io.shapereader.natural_earth(resolution='110m', category='cultural', 
                                                   name='admin_1_states_provinces_lakes_shp')
# read the shape file's contents
reader = cartopy.io.shapereader.Reader(states_files)

# line by line reading of data
data_line = reader.records()
data = next(data_line)
data
# grab individual bits of info
regions = lambda data: data.attributes['diss_me']
states_by_region = sorted(reader.records(),key=regions)[:5]
states_by_region
# lets move on from (badly spelled) academic pulling of data
fig = plt.figure()
ax = fig.add_subplot(111, projection=cartopy.crs.PlateCarree())
ax.coastlines()
plt.title('Equirectangular')
fig = plt.figure()
ax = fig.add_subplot(111,projection=cartopy.crs.Mollweide())
ax.coastlines()
plt.title("Mollweide")
# lets draw some lines between points
champaign_lat, champaign_lon = 40.1164, -88.2434
ant_lat, ant_lon = -18.8792, 47.5079
fig = plt.figure()
ax = fig.add_subplot(111, projection = cartopy.crs.PlateCarree())
ax.coastlines()
ax.gridlines()
ax.set_global()
ax.plot([champaign_lon, ant_lon],[champaign_lat,ant_lat], 
       transform=cartopy.crs.PlateCarree())
# shortest distance
ax.plot([champaign_lon, ant_lon],[champaign_lat,ant_lat], 
       transform=cartopy.crs.Geodetic())

# I can also plot points
fig =plt.figure()
ax=fig.add_subplot(111,projection=cartopy.crs.Mollweide())
champaign = 40.1164, -88.2434
oahu = 19.8968, -155.582
ax.scatter(champaign[1],champaign[0], 
          transform = cartopy.crs.PlateCarree())
ax.scatter(oahu[1],oahu[0], transform=cartopy.crs.PlateCarree())
ax.set_global()
ax.coastlines()
locations = pd.read_csv('/Users/jillnaiman1/Downloads/location.txt', header=None, delimiter='\t',
                       names=['longitude', 'latitude', 'empty1', 'empty2'])
del locations['empty1'], locations['empty2']
locations
fig = plt.figure()
ax = fig.add_subplot(111,projection=cartopy.crs.LambertCylindrical())
ax.scatter(locations['longitude'], locations['latitude'], transform=cartopy.crs.PlateCarree())
ax.coastlines()
#ax.set_global()
import cartopy.io.img_tiles
imagery = cartopy.io.img_tiles.OSM()

fig = plt.figure()
ax = fig.add_subplot(111,projection=cartopy.crs.LambertCylindrical())
ax.scatter(locations['longitude'], locations['latitude'], transform=cartopy.crs.PlateCarree())
ax.coastlines()
ax.add_image(imagery, 4)
seismic = pd.read_csv('/Users/jillnaiman1/Downloads/data_tohoku_norm_transpose.csv',
           header = None)
seismic.shape, locations.shape
14401/(60*60)
import ipywidgets
@ipywidgets.interact(station=(0,437))
def plot(station=0):
    plt.plot(seismic[station])
    plt.xlabel("Time in sec")
    plt.ylabel("Normalized Displacement")
    plt.ylim(-1,1)
nstations = 300
ntimes = 1440

import numpy as np
stationsIndex = np.random.choice(range(locations.shape[0]-1), nstations, replace=False)
timesIndex = np.random.choice(range(seismic.shape[0]-1), ntimes, replace=False)

stationsIndex.sort()
timesIndex.sort()

locations2 = locations.loc[stationsIndex]
seismic2 = seismic.loc[timesIndex, stationsIndex]
seismic2.shape, locations2.shape
fig = plt.figure()
ax = fig.add_subplot(111, projection=cartopy.crs.LambertCylindrical())
ax.scatter(locations2['longitude'], locations2['latitude'], transform=cartopy.crs.PlateCarree())
ax.coastlines()
seismic2
@ipywidgets.interact(station=(0,nstations-1))
def plot(station=0):
    plt.plot(seismic2.iloc[:,station])
    plt.xlabel("Time in sec")
    plt.ylabel("Normalized Displacement")
    plt.ylim(-1,1)
import bqplot
# scales
x_sc = bqplot.LinearScale()
y_sc = bqplot.LinearScale()

# marks
lines = bqplot.Lines(x=seismic2.index.values, y=seismic2.loc[:,0], 
                    scales={'x':x_sc, 'y':y_sc})

# axes
x_ax = bqplot.Axis(scale=x_sc)
y_ax = bqplot.Axis(scale=y_sc, orientation='vertical')

# combine into figure
fig = bqplot.Figure(marks=[lines], axes=[x_ax,y_ax])

# lets link a slider widget
slider = ipywidgets.IntSlider(min=0,max=nstations-1)

# create a linking function
def update_slider(event):
    lines.y = seismic2.iloc[:,event['new']]
    
slider.observe(update_slider,'value')

ipywidgets.VBox([slider, fig])
# combining sensor data as a function of time with our map data (locations)
@ipywidgets.interact(station=(0,nstations-1,1), t=(0,ntimes,1))
def plot(station=0, t=0):
    fig = plt.figure()
    # map figure
    ax=fig.add_subplot(211, projection=cartopy.crs.LambertCylindrical())
    colors=seismic2.iloc[t]
    ax.scatter(locations2['longitude'], locations2['latitude'], 
              transform=cartopy.crs.PlateCarree(), c=colors)
    ax.coastlines()
    
    # plot of a particular sensosr as a function of time
    ax = fig.add_subplot(212)
    ax.plot(seismic2.index.values, seismic2.iloc[:,station])
    ax.set_ylim(-1,1)<jupyter_output><empty_output><jupyter_text>Maps with bqplot<jupyter_code>map_mark = bqplot.Map(scales={'projection':bqplot.AlbersUSA()})
fig = bqplot.Figure(marks=[map_mark],title='Basic')
fig
# lets do state map instead
sc_geo = bqplot.AlbersUSA() # scales, USA
state_data = bqplot.topo_load('map_data/USStatesMap.json')

# lets add a tooltip that shows us something about the
#  underlying data
def_tt = bqplot.Tooltip(fields=['id','name'])


# generate marks
states_map = bqplot.Map(map_data=state_data, scales={'projection':sc_geo}, 
                       tooltip=def_tt)
states_map.interactions ={'hover':'tooltip', 'click':'select'}

from states_utils import get_ids_and_names
ids, state_names = get_ids_and_names(states_map)

def get_data_value(change):
    if change['owner'].selected is not None:
        for i, s in enumerate(change['owner'].selected):
            print(state_names[s==ids], s)
states_map.observe(get_data_value,'selected')

fig = bqplot.Figure(marks=[states_map], title='US States Map', 
                   fig_margin={'top':0, 'bottom':0, 'left':0, 'right':0})
fig
# lets link some export data to our map
comm = pd.read_csv('/Users/jillnaiman1/Downloads/total_export.csv')
comm
comm.loc[comm['State']=='Alabama'].values
years = list(comm.columns.values)
years = np.array(years[1:])
years = years.astype('int')
years
# lets do state map instead
sc_geo = bqplot.AlbersUSA() # scales, USA
state_data = bqplot.topo_load('map_data/USStatesMap.json')

# lets add a tooltip that shows us something about the
#  underlying data
def_tt = bqplot.Tooltip(fields=['id','name'])


# generate marks for map
states_map = bqplot.Map(map_data=state_data, scales={'projection':sc_geo}, 
                       tooltip=def_tt)
states_map.interactions ={'hover':'tooltip', 'click':'select'}

from states_utils import get_ids_and_names
ids, state_names = get_ids_and_names(states_map)

# line plot for exports
x_scl = bqplot.LinearScale()
y_scl = bqplot.LinearScale()
ax_xcl = bqplot.Axis(label='Year', scale=x_scl)
ax_ycl = bqplot.Axis(label='Total Export from State NA', scale=y_scl, orientation='vertical', side='left')
lines = bqplot.Lines(x=years, y=np.zeros(len(years)), scales={'x':x_scl, 'y':y_scl})
fig_lines = bqplot.Figure(marks=[lines],axes=[ax_ycl, ax_xcl])

def get_data_value(change):
    exports = np.zeros(len(years))
    snames =''
    if change['owner'].selected is not None:
        for i, s in enumerate(change['owner'].selected):
            sn = state_names[s == ids][0]
            snames += sn +', '
            exports_in = comm.loc[comm['State']==sn].values[0][1:]
            # I have commas in my strings
            exports_in = np.array([export_in[i].replace(',','') for i in range(length(export_in))])
            export = np.apd(exports,exports_in.astype('float64'))
            lines.y=exports
            ax_ycl.label='Total export from ' + snnames
            
            #print(state_names[s==ids], s)
states_map.observe(get_data_value,'selected')

fig = bqplot.Figure(marks=[states_map], title='US States Map', 
                   fig_margin={'top':0, 'bottom':0, 'left':0, 'right':0})

ipywidgets.HBox([fig,fig_lines])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14701954595119582, 'fraction_numerical': 0.020881388855165717, 'mean_word_length': 4.92235801581596, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '300371', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 3069, 'n_tokens_neox': 2921, 'n_words': 540, 'path': 'week08/.ipynb_checkpoints/inClass_week08-checkpoint.ipynb', 'repo_name': 'UIUC-iSchool-DataViz/spring2019online', 'size': '8237'}