Updating Y-axis Scale and Title from Callback functions

I am trying to update my Bokeh plots using different widgets. I have been updating the ColumnDataSource and the data has been updated successfully, but anything other than the data points isnt being updated on my Bokeh plots. In specific, I am trying to update the Y-axis scale and the title of my plots from the ColumnDataSource, and they do not get updated from my callback function. Below is an example of how I am trying to update the Y-axis scale.

   #scaling the y-axis
   minimum_ranges = {'WAC': 0.25, 'WALA': 15, 'AOLS': 25000, 'WAOLT':10, 'WAOLTV':10, 'WAOCS': 25}
   if y in minimum_ranges: 
      data_range = data_source.data[y].max() - data_source.data[y].min()
      if data_range < minimum_ranges[y]: 
         scale_diffrence = minimum_ranges[y] - data_range 
         p.y_range.start = data_source.data[y].min() - scale_diffrence/2
         p.y_range.end = data_source.data[y].max() + scale_diffrence/2

My callback just generates a new pandaframe, and updates the ColumnDataSource accordingly.

def callback(attr,old, new): 
   story = story_text.value
   coupon = coupon_text.value
   vintage = vintage_text.value
      cb_df = generate_df(start_date, end_date, vintage, coupon, story)
       cohorts_source.data.update({'key1': ["1MO", "3MO", "6MO", "12MO"],
                               'key2': [round(cb_df[var][0]*100, 2) for var in ["CPR_1MO", "CPR_3MO", "CPR_6MO", "CPR_12MO"]]})
      CPR_plot.title = "NEW TITLE"

Do you have any suggestions for how I can update the Y-axis and the title of my plots based on widgets data?

Hi @bdaqqah

A runnable minimal reproducible example would be useful to provide more precise and helpful feedback on possible causes for the behavior reported.

From the excerpts of code available, the following observations might help. Addressing the questions in reverse order …

TITLE: The bokeh figure constructor allows you to specify a title via a string argument when instantiating the figure, e.g. p = figure(..., title='MY TITLE')

However, if you look at the figure object returned from that assignment, i.e. p, the title property is actually a Title object.

So, in your callback function, assuming that CPR_plot is a bokeh figure object, you’d want to set the text property of the Title object as such

CPR_plot.title.text = "NEW TITLE"

Y-Axis SCALE: A bit more speculation here absent complete code, but it appears that you have manually set the ranges based on data and limits.

These manual (start,stop) points will take precedence and keep bokeh from auto-scaling when new data are added to the plot. The natural assumption there is that because manual ranges were explicitly set, this is what the user wants to persist.

Your callback doesn’t do anything to update the y-axis range, so therefore no change. You’ll need to work logic into your callback to handle this if you need to have logic that limits based on some application knowledge.

1 Like