Toggle between Log and Linear scale

Hi,

Is it possible to change the y_axis_type via a button? I tried the below code and tried to find the y_axis_type in the list of attributes but cant seem to find it. Anyone have an idea?

import numpy as np
from bokeh.models import CustomJS, RadioButtonGroup
from bokeh.plotting import figure, show

LABELS = ["Log Scale", "Linear Scale"]

p = figure(title="log axis example", y_axis_type="log",
           background_fill_color="#fafafa")

x = np.linspace(0.1, 5, 80)

p.line(x, x, legend_label="y=x")
p.circle(x, x, legend_label="y=x")

p.legend.location = "top_left"

radio_button_group = RadioButtonGroup(labels=LABELS, active=0)
radio_button_group.js_on_click(CustomJS(args=dict(p=p), code="""
    console.log('radio_button_group: active=' + this.active, this.toString())
    console.log(p)
"""))

show(column(p, radio_button_group))

Hi @Zana,

Switching between scales in an existing plot is not possible. The advice here is to create two plots and toggle visibility instead. Here’s a simple example:

from bokeh.plotting import figure, show
from bokeh.layouts import column
from bokeh.models import RadioButtonGroup, CustomJS

LABELS = ["Log Scale", "Linear Scale"]
f1 = figure(title="log axis example", y_axis_type="log")
f2 = figure(title="linear axis example", y_axis_type="linear")

x = [0, 1, 2, 3, 4, 5]
y = [20, 40, 80, 160, 320, 640]
f1.line(x=x, y=y)
f2.line(x=x, y=y)

f1.visible = True  # this is default, so this line can be removed
f2.visible = False

radio_button_group = RadioButtonGroup(labels=LABELS, active=0)
col = column(children=[f1, f2, radio_button_group])
radio_button_group.js_on_click(CustomJS(args=dict(f1=f1, f2=f2), code="""
  if (f1.visible == true) {
    f1.visible = false;
    f2.visible = true;
  }
  else {
    f1.visible = true;
    f2.visible = false;
  }
"""))

show(col)
1 Like