Widgets and Interactions


In [1]:
# To enter bash shell commands please include an exclamation mark
#!conda install -y netcdf4

In [2]:
from netCDF4 import Dataset, num2date, date2num
from numpy import *
import matplotlib.pyplot as plt
%matplotlib inline

In [3]:
from ipywidgets import interact, interactive, fixed
import ipywidgets as widgets

In [4]:
x=linspace(0,1,100)
f=2

In [5]:
plt.plot(x, sin(2*pi*x*f))


Out[5]:
[<matplotlib.lines.Line2D at 0x10b81bef0>]

In [6]:
def pltsin(f):
    plt.plot(x, sin(2*pi*x*f))

In [7]:
pltsin(0.5)



In [8]:
interact(pltsin, f=(1,10,0.1))


Out[8]:
<function __main__.pltsin>

Add to the function to allow amplitude to varied and add in an addtional slider to vary both f and a


In [9]:
def pltsin (f,a):
    plt.plot(x,a * sin(2*pi*x*f))
    plt.ylim(-10.)

In [10]:
interact (pltsin, f=(1,10,0.1), a=(1,10,0.1))


Out[10]:
<function __main__.pltsin>

Climate Data


In [11]:
f=Dataset('ncep-data/air.sig995.2013.nc')

In [12]:
air=f.variables['air'] #Get variable

In [13]:
plt.imshow(air[0,:,:]) #Display first timestamp


Out[13]:
<matplotlib.image.AxesImage at 0x10baf4f28>

In [14]:
# Create function to browse through the days

def sh(time):
    plt.imshow(air[time,:,:])

In [15]:
# Now make it interactive
interact(sh, time=(0,355,1))


Out[15]:
<function __main__.sh>

In [17]:
# Browser Variable

def sh(var='air', time=0):
    f=Dataset('ncep-data/'+var+'.sig995.2013.nc')
    vv=f.variables[var]
    plt.imshow(vv[time,:,:])

In [18]:
# Give a list of variables

variabs = ['air','uwnd','vwnd','rhum']

In [19]:
# Now interact with it

interact(sh, time=(0,355,1), var=variabs)


Add functionality to interact with the years


In [20]:
def sh(year='2013', var='air', time=0):
    f=Dataset('ncep-data/'+var+'.sig995.2013.nc')
    vv=f.variables[var]
    plt.imshow(vv[time,:,:])

In [21]:
years =[str(x) for x in range(2013,2016)]
years =['2013','2014','2015']

In [22]:
# Interact with it
interact(sh, year=years, time=(0,355,1), var=variabs)



In [ ]: