Skip to content

Instantly share code, notes, and snippets.

@martindurant
Created May 23, 2018 16:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martindurant/f25ae1a5d8a6e1c2e608694cf541046e to your computer and use it in GitHub Desktop.
Save martindurant/f25ae1a5d8a6e1c2e608694cf541046e to your computer and use it in GitHub Desktop.
interactive image
import bokeh
from bokeh.io import output_notebook, push_notebook
output_notebook()
##
import numpy as np
from ipywidgets import interact, widgets
from bokeh.plotting import figure, show
from bokeh.models import LogColorMapper, LogTicker, ColorBar, HoverTool
from bokeh.layouts import column
from bokeh.models.widgets import RangeSlider
def normal2d(X, Y, sigx=1.0, sigy=1.0, mux=0.0, muy=0.0):
z = (X-mux)**2 / sigx**2 + (Y-muy)**2 / sigy**2
return np.exp(-z/2) / (2 * np.pi * sigx * sigy)
X, Y = np.mgrid[-3:3:100j, -2:2:100j]
Z = normal2d(X, Y, 0.1, 0.2, 1.0, 1.0) + 0.1*normal2d(X, Y, 1.0, 1.0)
image = Z * 1e6
color_mapper = LogColorMapper(palette="Viridis256", low=1, high=1e7)
plot = figure(x_range=(0,1), y_range=(0,1),
tools=[HoverTool(tooltips=[("x", "$x"), ("y", "$y"), ("value", "@image")])],
toolbar_location=None)
plot.image(image=[image], color_mapper=color_mapper,
dh=[1.0], dw=[1.0], x=[0], y=[0])
color_bar = ColorBar(color_mapper=color_mapper, ticker=LogTicker(),
label_standoff=12, border_line_color=None, location=(0,0))
plot.add_layout(color_bar, 'right')
t = show(plot, notebook_handle=True)
def update(colours):
color_mapper.low, color_mapper.high = colours
push_notebook(color_mapper)
cols = widgets.FloatRangeSlider(min=1,max=1e7,value=[1, 1e7])
interact(update, colours=cols);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment