I hope use on_change to change my figure's x_range

here my plot code:

source = ColumnDataSource(data=dict(x=,y=,height=))
plot = figure(plot_height=600, plot_width=800, title="", toolbar_location=None, tools=[hover],x_range=industry,y_range=[0,2000])

p=plot.rect(x=‘x’,y=‘y’,width=.8,height=‘height’,source=source)

I have three different x_range which should be changed when I select different x_axis:

x_axis=Select(title=‘X axis’,options=sorted(axis_map.keys()),value=‘Current Industry’)

here is my update function:

def update(attrname,old,new):

plot.xaxis.axis_label=x_axis.value

plot.yaxis.axis_label=‘numbers’

source.data=dict(

x=axis_map[x_axis.value],

y=height_map[x_axis.value],

height=height_map[x_axis.value]*2.0

)

question:

I wish to change my rect’s x value and figure’s x_range value at the same time in every update. But I can’t find a way to it.

Or can I just repaint the whole figure and rect every time I select a new x_axis…
I really don’t know how to continue, I have been trapped here by two days

在 2016年6月5日星期日 UTC+8上午11:58:16,Qiwei Peng写道:

···

here my plot code:

plot = figure(plot_height=600, plot_width=800, title="", toolbar_location=None, tools=[hover],x_range=industry,y_range=[0,2000])

p=plot.rect(x=‘x’,y=‘y’,width=.8,height=‘height’,source=source)

I have three different x_range which should be changed when I select different x_axis:

x_axis=Select(title=‘X axis’,options=sorted(axis_map.keys()),value=‘Current Industry’)

here is my update function:

def update(attrname,old,new):

plot.xaxis.axis_label=x_axis.value

plot.yaxis.axis_label=‘numbers’

source.data=dict(

x=axis_map[x_axis.value],

y=height_map[x_axis.value],

height=height_map[x_axis.value]*2.0

)

question:

I wish to change my rect’s x value and figure’s x_range value at the same time in every update. But I can’t find a way to it.

source = ColumnDataSource(data=dict(x=,y=,height=))

Hello Peng,

Sorry you’ve been stuck for so long.

I have been having a similar issue myself and might have a solution.

Using the code bellow I was able to successfully change both the x ranges and value on my graph:

···

===================================================================

from bokeh.models import ColumnDataSource, Range1d

from bokeh.plotting import figure, vplot

from bokeh.io import output_file, save

from bokeh.models.widgets import Select

X_VALUES = {

‘X1’ : [0,2,3,4,5,6],

‘X2’ : [1,2,9,4,6,7],

‘X3’ : [2,0,0,1,2,3]

}

select = Select(title=‘X Axes’, value=‘X1’, options=[‘X1’, ‘X2’, ‘X3’])

source = ColumnDataSource(data=dict(x=X_VALUES[‘X1’], y=[1,2,9,4,6,7]))

output_file(‘changeaxisHTML.html’)

p = figure(width=500, height=500)

p.rect(x=‘x’, y=‘y’, source=source, height=[2]*6, width=[0.3]*6)

layout = vplot(p,select, width=p.plot_width)

def update (attrname, old, new):

source.data[‘x’] = X_VALUES[select.value]

select.on_change(‘value’, update)

save(layout)

===============================================================

As you can see I had my dictionary of x value lists and initially assigned one (X1) to my source for the initial display. After that, I made sure my update changed source.data to match the slider. After doing this the x_range automatically updated to easily display the new data. Unfortunately there is no way of manually setting the range that I know. You can write to the p.x_range value, but the figure will not update it so it is best to just let Bokeh automatically handle it.

I hope this helps!

NOTE: Because I am using a Python update function the code must be run using Bokeh serve

I was incorrect in saying you cannot change the ranges. You cannot assign a new Range1d and have it update, but if you change the Range1d.start and Range1d.end values the range of the graph will update.