Widges and Interactions


In [1]:
!conda install -y netcdf4


Fetching package metadata .........
Solving package specifications: .

# All requested packages already installed.
# packages in environment at /Users/chanitacr/anaconda:
#
netcdf4                   1.2.4               np111py35_1  

In [14]:
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 0x110a44080>]

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

In [7]:
pltsin(5)


Interaccion empieza en 1 va a hasta 10 y se devuelve hasta 1 En esta interaccion esta disponible solo para Jypiter no para terminal

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


Out[8]:
<function __main__.pltsin>

Add to the funtion to allow amplitude to be varied and add in an additional slider to vary both f and a

Hint you might want to limit the magnitude of 'y' in the plot - look up matplotlib


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

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


Out[10]:
<function __main__.pltsin>

Climate data


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

In [17]:
air=f.variables['air'] #get variable

In [25]:
plt.imshow(air[364,:,:])  #display first timestep


Out[25]:
<matplotlib.image.AxesImage at 0x108350710>

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

In [24]:
#now make it interactive

interact(sh, time=(0,364,1))



In [36]:
# Browse variable

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

In [37]:
# Give a list of varibles

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

In [38]:
# Now interact with it

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


Out[38]:
<function __main__.sh>

In [49]:
# Browse variable

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

In [50]:
# Create a list of years

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

In [51]:
# Give a list of varibles

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

In [53]:
# Now interact with it

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



In [ ]: