Twin x-axis link - what am I doing wrong?

Hi,

In the code below I have created two plots and they share the x axis. How can I have the top plot share all x axis. If I click on the select tool and move the curve around it doesnt move all the x ticks around. What am I doing wrong here?

from numpy import arange, linspace, pi, sin

from bokeh.models import LinearAxis, Range1d
from bokeh.plotting import figure, output_file, show

t = np.linspace(0, 30000, 100) #hours
A, n = 1, -0.65
y = A*np.power(t, n)

p = figure(width = 1600, height = 300, x_range=(t.min(), t.max()))
p2 = figure(width = 1600, height = 300, x_range=p.x_range)

p.background_fill_color = "#fafafa"

p.circle(t, y, color="crimson", size=8)
p.xaxis.axis_label = "Time [hrs]"
p.xaxis.axis_label_text_color ="crimson"

p2.circle(t, y*5, color="crimson", size=8)
p2.xaxis.visible = False

p.extra_x_ranges['days'] = Range1d(t.min()/24, t.max()/24)
ax2 = LinearAxis(x_range_name="days", axis_label="Time [days]")
ax2.axis_label_text_color ="navy"
p.add_layout(ax2, 'below')

p.extra_x_ranges['years'] = Range1d(t.min()/(24*365), t.max()/(24*365))
ax3 = LinearAxis(x_range_name="years", axis_label="Time [years]")
ax3.axis_label_text_color ="green"
p.add_layout(ax3, 'below')

output_file("twin_axis.html", title="twin_axis.py example")

show(column(p2, p))

Just add p2.extra_x_ranges = p.extra_x_ranges right above the call to output_file.

1 Like