Install the lastest widgets code...

conda install -c conda-forge ipywidgets


In [39]:
from ipywidgets import Dropdown, RadioButtons, IntSlider, VBox, FloatSlider, Button
from IPython.display import display

In [28]:
data_type = RadioButtons(options=['sequential', 'diverging', 'qualitative'])

bindings = {'sequential': range(3,9+1),
            'diverging': range(3,11+1),
            'qualitative': range(3,12+1)}

class_val = Dropdown(options=bindings[data_type.value])    

def type_change(change):
    class_val.options = bindings[change['new']]

data_type.observe(type_change, names=['value'])

display(VBox([data_type, class_val]))

In [29]:
"""
data_type = RadioButtons(options=['sequential', 'diverging', 'qualitative'])

bindings = {'sequential': range(3,9+1),
            'diverging': range(3,11+1),
            'qualitative': range(3,12+1)}

class_val = Dropdown(options=bindings[data_type.value])    

def type_change(change):
    class_val.options = bindings[change['new']]

data_type.observe(type_change, names=['value'])

display(VBox([data_type, class_val]))
"""
# on data_type change update list of color maps
# based on class_val and data_type

from pysal.contrib.viz.color import color_display_types

dt = sorted(color_display_types.keys(), reverse=True)
data_type = RadioButtons(description='Data Type', options=dt)
bindings = {}
for t in dt:
    bindings[t] = color_display_types[t].keys()
    
cmap = Dropdown(description='CMap:', options=bindings[data_type.value])

def type_change(change):
    with cmap.hold_trait_notifications():
        cmap.options = bindings[change['new']]

data_type.observe(type_change, names=['value'])

display(VBox([data_type, cmap]))

In [30]:
"""
data_type = RadioButtons(options=['sequential', 'diverging', 'qualitative'])

bindings = {'sequential': range(3,9+1),
            'diverging': range(3,11+1),
            'qualitative': range(3,12+1)}

class_val = Dropdown(options=bindings[data_type.value])    

def type_change(change):
    class_val.options = bindings[change['new']]

data_type.observe(type_change, names=['value'])

display(VBox([data_type, class_val]))
"""
# on data_type change update list of color maps
# based on class_val and data_type

from pysal.contrib.viz.color import color_display_types
from pysal.contrib.viz.color import plot_cmaps as pcmaps

dt = sorted(color_display_types.keys(), reverse=True)
data_type = RadioButtons(description='Data Type', options=dt)
bindings = {}
for t in dt:
    bindings[t] = color_display_types[t].keys()
    
cmap = Dropdown(description='CMap:', options=bindings[data_type.value])

def type_change(change):
    with cmap.hold_trait_notifications():
        cmap.options = bindings[change['new']]

data_type.observe(type_change, names=['value'])  

display(VBox([data_type, cmap]))

In [31]:
%matplotlib inline
from pysal.contrib.viz.color import plot_cmaps 
plot_cmaps('diverging', selected=3)



In [90]:
"""
data_type = RadioButtons(options=['sequential', 'diverging', 'qualitative'])

bindings = {'sequential': range(3,9+1),
            'diverging': range(3,11+1),
            'qualitative': range(3,12+1)}

class_val = Dropdown(options=bindings[data_type.value])    

def type_change(change):
    class_val.options = bindings[change['new']]

data_type.observe(type_change, names=['value'])

display(VBox([data_type, class_val]))
"""
# on data_type change update list of color maps
# based on class_val and data_type

from pysal.contrib.viz.color import color_display_types
from pysal.contrib.viz.color import plot_cmaps as pcmaps
from ipywidgets import interact

dt = sorted(color_display_types.keys(), reverse=True)
data_type = RadioButtons(description='Data Type', options=dt)
bindings = {}
for t in dt:
    bindings[t] = color_display_types[t].keys()
    
cmap_dd = Dropdown(description='CMap:', options=bindings[data_type.value])

def type_change(change):
    with cmap_dd.hold_trait_notifications():
        cmap_dd.options = bindings[change['new']]
        #plot_cmaps('diverging')

def cmap_change(change):
        with cmap_dd.hold_trait_notifications():
            print('new cmap', str(change['new']))

data_type.observe(type_change, names=['value'])  
cmap_dd.observe(cmap_change, names=['value'])

#display(VBox([data_type, cmap]))
@interact(cmap=cmap_dd, data_type=data_type)
def plot_cmaps(data_type, cmap):
    #print(data_type,cmap)
    i = cmap_dd.options.index(cmap)
    pcmaps(data_type, i)



In [95]:
"""
data_type = RadioButtons(options=['sequential', 'diverging', 'qualitative'])

bindings = {'sequential': range(3,9+1),
            'diverging': range(3,11+1),
            'qualitative': range(3,12+1)}

class_val = Dropdown(options=bindings[data_type.value])    

def type_change(change):
    class_val.options = bindings[change['new']]

data_type.observe(type_change, names=['value'])

display(VBox([data_type, class_val]))
"""
# on data_type change update list of color maps
# based on class_val and data_type

from pysal.contrib.viz.color import color_display_types
from pysal.contrib.viz.color import plot_cmaps as pcmaps
from ipywidgets import interact

dt = sorted(color_display_types.keys(), reverse=True)
data_type = RadioButtons(description='Data Type', options=dt)
bindings = {}
for t in dt:
    bindings[t] = color_display_types[t].keys()
    
cmap_dd = Dropdown(description='CMap:', options=bindings[data_type.value])

def type_change(change):
    with cmap_dd.hold_trait_notifications():
        cmap_dd.options = bindings[change['new']]
        #plot_cmaps('diverging')

def cmap_change(change):
        with cmap_dd.hold_trait_notifications():
            print('new cmap', str(change['new']))

data_type.observe(type_change, names=['value'])  
cmap_dd.observe(cmap_change, names=['value'])

display(VBox([data_type]))
@interact(cmap=cmap_dd)
def plot_cmaps(cmap):
    #print(data_type,cmap)
    i = cmap_dd.options.index(cmap)
    pcmaps(data_type.value, i)



In [103]:
# adding k

# on data_type change update list of color maps
# based on class_val and data_type

from pysal.contrib.viz.color import color_display_types
from pysal.contrib.viz.color import plot_cmaps as pcmaps
from pysal.contrib.viz.color import get_color_map
from ipywidgets import interact

dt = sorted(color_display_types.keys(), reverse=True)
data_type = RadioButtons(description='Data Type', options=dt)
bindings = {}
for t in dt:
    bindings[t] = color_display_types[t].keys()
    
cmap_dd = Dropdown(description='CMap:', options=bindings[data_type.value])

def type_change(change):
    with cmap_dd.hold_trait_notifications():
        cmap_dd.options = bindings[change['new']]
        k_dd.options = kbindings[change['new']]
        #plot_cmaps('diverging')

def cmap_change(change):
        with cmap_dd.hold_trait_notifications():
            print('new cmap', str(change['new']))

data_type.observe(type_change, names=['value'])  
cmap_dd.observe(cmap_change, names=['value'])
kbindings = {'sequential': range(3,9+1),
            'qualitative': range(3,12+1),
            'diverging': range(3,11+1)}
k_dd = Dropdown(description='k', options=kbindings[data_type.value])

display(VBox([data_type, k_dd]))
@interact(cmap=cmap_dd)
def plot_cmaps(cmap):
    #print(data_type,cmap)
    i = cmap_dd.options.index(cmap)
    pcmaps(data_type.value, i)
    print(data_type.value,cmap, k_dd.value)


('qualitative', u'Pastel1', 5)

In [108]:
# adding button

# on data_type change update list of color maps
# based on class_val and data_type

from pysal.contrib.viz.color import color_display_types
from pysal.contrib.viz.color import plot_cmaps as pcmaps
from pysal.contrib.viz.color import get_color_map
from ipywidgets import interact

dt = sorted(color_display_types.keys(), reverse=True)
data_type = RadioButtons(description='Data Type', options=dt)
bindings = {}
for t in dt:
    bindings[t] = color_display_types[t].keys()
    
cmap_dd = Dropdown(description='CMap:', options=bindings[data_type.value])

def type_change(change):
    with cmap_dd.hold_trait_notifications():
        cmap_dd.options = bindings[change['new']]
        k_dd.options = kbindings[change['new']]
        #plot_cmaps('diverging')

def cmap_change(change):
        with cmap_dd.hold_trait_notifications():
            print('new cmap', str(change['new']))

data_type.observe(type_change, names=['value'])  
cmap_dd.observe(cmap_change, names=['value'])
kbindings = {'sequential': range(3,9+1),
            'qualitative': range(3,12+1),
            'diverging': range(3,11+1)}
k_dd = Dropdown(description='k', options=kbindings[data_type.value])
def button_clicked(btn):
    print(data_type.value, cmap_dd.value, k_dd.value)

btn = Button(description="Display Map")
btn.on_click(button_clicked)
display(btn)
display(VBox([data_type, k_dd]))
@interact(cmap=cmap_dd)
def plot_cmaps(cmap):
    #print(data_type,cmap)
    i = cmap_dd.options.index(cmap)
    pcmaps(data_type.value, i)
    #print(data_type.value,cmap, k_dd.value)


('sequential', u'Purples', 4)
('sequential', u'Purples', 3)

In [109]:
# adding map

# on data_type change update list of color maps
# based on class_val and data_type

from pysal.contrib.viz.color import color_display_types
from pysal.contrib.viz.color import plot_cmaps as pcmaps
from pysal.contrib.viz.color import get_color_map
from ipywidgets import interact

dt = sorted(color_display_types.keys(), reverse=True)
data_type = RadioButtons(description='Data Type', options=dt)
bindings = {}
for t in dt:
    bindings[t] = color_display_types[t].keys()
    
cmap_dd = Dropdown(description='CMap:', options=bindings[data_type.value])

def type_change(change):
    with cmap_dd.hold_trait_notifications():
        cmap_dd.options = bindings[change['new']]
        k_dd.options = kbindings[change['new']]
        #plot_cmaps('diverging')

def cmap_change(change):
        with cmap_dd.hold_trait_notifications():
            print('new cmap', str(change['new']))

data_type.observe(type_change, names=['value'])  
cmap_dd.observe(cmap_change, names=['value'])
kbindings = {'sequential': range(3,9+1),
            'qualitative': range(3,12+1),
            'diverging': range(3,11+1)}
k_dd = Dropdown(description='k', options=kbindings[data_type.value])
def button_clicked(btn):
    print(data_type.value, cmap_dd.value, k_dd.value)

btn = Button(description="Display Map")
btn.on_click(button_clicked)
display(btn)
display(VBox([data_type, k_dd]))
@interact(cmap=cmap_dd)
def plot_cmaps(cmap):
    #print(data_type,cmap)
    i = cmap_dd.options.index(cmap)
    pcmaps(data_type.value, i)
    #print(data_type.value,cmap, k_dd.value)



In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [33]:
plot_cmaps('diverging', 5)



In [34]:
plot_cmaps('sequential', 10)



In [47]:
from ipywidgets import Text

int_range = IntSlider()
display(int_range)

text = Text()
display(text)

def on_value_change(change):
    print(change['new'])
    text.value = str(change['new'])
    
    
int_range.observe(on_value_change, names='value')


1
2
3
4
5
6
7

In [35]:
"""
data_type = RadioButtons(options=['sequential', 'diverging', 'qualitative'])

bindings = {'sequential': range(3,9+1),
            'diverging': range(3,11+1),
            'qualitative': range(3,12+1)}

class_val = Dropdown(options=bindings[data_type.value])    

def type_change(change):
    class_val.options = bindings[change['new']]

data_type.observe(type_change, names=['value'])

display(VBox([data_type, class_val]))
"""
# on data_type change update list of color maps
# based on class_val and data_type

from pysal.contrib.viz.color import color_display_types

dt = sorted(color_display_types.keys(), reverse=True)
data_type = RadioButtons(description='Data Type', options=dt)
bindings = {}
for t in dt:
    bindings[t] = color_display_types[t].keys()
    
cmap = Dropdown(description='CMap:', options=bindings[data_type.value])

def type_change(change):
    with cmap.hold_trait_notifications():
        cmap.options = bindings[change['new']]

data_type.observe(type_change, names=['value'])

display(VBox([data_type, cmap]))

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [12]:
x_widget = FloatSlider(min=0.0, max=10.0, step=0.05)
y_widget = FloatSlider(min=0.5, max=10.0, step=0.05, value=5.0)

def update_x_range(*args):
    x_widget.max = 2.0 * y_widget.value
y_widget.observe(update_x_range, 'value')

def printer(x, y):
    print(x, y)
interact(printer,x=x_widget, y=y_widget)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-12-6677e49d3878> in <module>()
      8 def printer(x, y):
      9     print(x, y)
---> 10 interact(printer,x=x_widget, y=y_widget)

NameError: name 'interact' is not defined

In [ ]:


In [ ]:


In [ ]:


In [ ]:
import pysal as ps
import numpy as np
import matplotlib.pyplot as plt
from scipy.linalg import inv
%matplotlib inline

In [ ]:
from ipywidgets import interact
interact(draw_map, lamb=(-0.9, 0.9))

In [ ]:
def draw_map(lamb):
    s = 20
    n = s**2
    w = ps.lat2W(s, s, rook=False)
    w.transform = 'R'
    e = np.random.random((n, 1))
    u = inv(np.eye(n) - lamb * w.full()[0])
    u = np.dot(u, e)
    ul = ps.lag_spatial(w, u)
    u = (u - u.mean()) / np.std(u)
    ul = (ul - ul.mean()) / np.std(ul)
    gu = u.reshape((s, s))
    # Figure
    f = plt.figure(figsize=(9, 4))
    ax1 = f.add_subplot(121)
    ax1.matshow(gu, cmap=plt.cm.YlGn)
    ax1.set_frame_on(False)
    ax1.axes.get_xaxis().set_visible(False)
    ax1.axes.get_yaxis().set_visible(False)
    #---
    ax2 = f.add_subplot(122)
    sc = ax2.scatter(u, ul, linewidth=0)
    ols = ps.spreg.OLS(ul, u)
    tag = "b = %.3f"%ols.betas[1][0]
    ax2.plot(u, ols.predy, c='red', label=tag)
    ax2.axvline(0, c='0.5')
    ax2.axhline(0, c='0.5')
    ax2.legend()
    plt.xlabel('u')
    plt.ylabel('Wu')
    plt.suptitle("$\lambda$ = %.2f"%lamb)
    plt.show()

In [ ]: