-
-
Save gileshd/106a8051b0d7bd2c3696955c298e5f80 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from bokeh.layouts import row, widgetbox | |
from bokeh.models import ColumnDataSource, LinearColorMapper, Select, ColorBar | |
from bokeh.plotting import figure, curdoc | |
from bokeh.palettes import Viridis256 | |
from bokeh.sampledata import iris | |
df = iris.flowers | |
data_options = sorted(df.columns)[:-1] | |
p = figure(plot_height=400, plot_width=500, toolbar_location=None, id='fig') | |
# Select widgets | |
x_select = Select(title='X axis', options=data_options, value=data_options[0]) | |
y_select = Select(title='Y axis', options=data_options, value=data_options[1]) | |
color_select = Select(title='Color', options=data_options, value=data_options[2]) | |
selector_dict = {'x': x_select, 'y': y_select} | |
def update_source(attr, old, new, column): | |
# choose which select widget to look at | |
selector = selector_dict[column] | |
# make a dictionary of the column and new data | |
data_update_dict = {column: df[selector.value]} | |
# update the source | |
source.data.update(data_update_dict) | |
def update_color(attr, old, new): | |
source.data.update({'color': df[color_select.value]}) | |
color_mapper.high = df[color_select.value].max() | |
color_mapper.low = df[color_select.value].min() | |
# set the on_change functions | |
x_select.on_change('value', lambda attr, old, new: update_source(attr, old, new, column = 'x')) | |
y_select.on_change('value', lambda attr, old, new: update_source(attr, old, new, column = 'y')) | |
color_select.on_change('value', update_color) | |
color_mapper = LinearColorMapper(low=df[color_select.value].min(), | |
high=df[color_select.value].max(), | |
palette=Viridis256, | |
id='cmpr') | |
source = ColumnDataSource({'x': df[x_select.value], | |
'y': df[y_select.value], | |
'color': df[color_select.value]}) | |
# add circle glyphs | |
r = p.circle('x', 'y', | |
size=8, | |
color={'field': 'color', 'transform': color_mapper}, | |
source=source, id='circles') | |
color_bar = ColorBar(color_mapper=color_mapper, | |
label_standoff=5, | |
location=(10, 0)) | |
# put the pieces together | |
p.add_layout(color_bar, 'right') | |
controls = widgetbox([x_select, y_select, color_select], | |
width=200) | |
layout = row(controls, p) | |
curdoc().add_root(layout) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Plot changes size when petal_length is chosen to color the glyphs. Does not occur for any other column.