interactively adjust color_mapper min and max

Hi I would like to build an interactive map for oceanographic data exploration.

Basically I want to show several maps which all have linked ranges and colorbars.

I am having trouble implementing an interactive control of the colorlimits.

What I want is a widgetbox with a color_min and color_max slider (or even better a single RangeSlider) to adjust the color_mapper properties min and max.

This is what I have (mostly based on this: https://github.com/bokeh/bokeh/issues/4612), but the figure is not updated when I use the sliders?

import numpy as np

from bokeh.plotting import figure, curdoc, show, output_notebook

from bokeh.layouts import row, layout,widgetbox

from bokeh.models import Slider

from bokeh.models.mappers import LinearColorMapper

from bokeh.palettes import Greys9

def change_image_contrast(attr, old, new):

fig_im.glyph.color_mapper.update(low=graph_min_slider.value, high=graph_max_slider.value)

fig_im.trigger(‘glyph’, fig_im.glyph, fig_im.glyph)

graph_min_slider = Slider(title=“Min”, start=0, end=99, step=1, value=0)

graph_max_slider = Slider(title=“Max”, start=1, end=100, step=1, value=100)

graph_min_slider.on_change(‘value’, change_image_contrast)

graph_max_slider.on_change(‘value’, change_image_contrast)

fig = figure(plot_width=500, plot_height=500, x_range=(0, 10), y_range=(0, 10))

fig_im = fig.image(image=[np.random.randint(0, 100, (10, 10), dtype=‘int16’)], x=[0], y=[0], dw=[10], dh=[10],

color_mapper=LinearColorMapper(low=0, high=100, palette=Greys9))

layout = row(

fig,widgetbox(graph_min_slider,graph_max_slider),

)

show(layout)

``

Am I approaching this wrong? I am very new to bokeh and would be grateful for any advice.

Your approach only needs a small tweak. If you change the last line of your code from show(layout) to curdoc().add_root(layout) and run it from the terminal using Bokeh Server $ bokeh serve --show yourscript.py, it appears to work as intended.

Python callbacks like those in your code don’t work with the stand-alone html+javascript that is generated when the show() method is called. However, they do work with Bokeh Server — which you can read more about here and here.

If you are looking for a solution where you can output your work to a stand-alone html file or in line in a jupyter notebook, you can also try rewriting your callbacks as javascipt callbacks using CustomJS(). Javascript callbacks offer less flexibility but allow you to write callbacks directly to the html file that is ouput by the show() method. Info on js callbacks can be found here and here.

···

On Fri, Feb 17, 2017 at 1:52 PM, [email protected] wrote:

Hi I would like to build an interactive map for oceanographic data exploration.

Basically I want to show several maps which all have linked ranges and colorbars.

I am having trouble implementing an interactive control of the colorlimits.

What I want is a widgetbox with a color_min and color_max slider (or even better a single RangeSlider) to adjust the color_mapper properties min and max.

This is what I have (mostly based on this: https://github.com/bokeh/bokeh/issues/4612), but the figure is not updated when I use the sliders?

import numpy as np

from bokeh.plotting import figure, curdoc, show, output_notebook

from bokeh.layouts import row, layout,widgetbox

from bokeh.models import Slider

from bokeh.models.mappers import LinearColorMapper

from bokeh.palettes import Greys9

def change_image_contrast(attr, old, new):

fig_im.glyph.color_mapper.update(low=graph_min_slider.value, high=graph_max_slider.value)

fig_im.trigger(‘glyph’, fig_im.glyph, fig_im.glyph)

graph_min_slider = Slider(title=“Min”, start=0, end=99, step=1, value=0)

graph_max_slider = Slider(title=“Max”, start=1, end=100, step=1, value=100)

graph_min_slider.on_change(‘value’, change_image_contrast)

graph_max_slider.on_change(‘value’, change_image_contrast)

fig = figure(plot_width=500, plot_height=500, x_range=(0, 10), y_range=(0, 10))

fig_im = fig.image(image=[np.random.randint(0, 100, (10, 10), dtype=‘int16’)], x=[0], y=[0], dw=[10], dh=[10],

color_mapper=LinearColorMapper(low=0, high=100, palette=Greys9))

layout = row(

fig,widgetbox(graph_min_slider,graph_max_slider),

)

show(layout)

``

Am I approaching this wrong? I am very new to bokeh and would be grateful for any advice.

You received this message because you are subscribed to the Google Groups “Bokeh Discussion - Public” group.

To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

To post to this group, send email to [email protected].

To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/f6da2d7e-9b88-4862-917f-ae2517393fe4%40continuum.io.

For more options, visit https://groups.google.com/a/continuum.io/d/optout.