39

Does anyone know how to add x and y axis title/labels for a Bokeh figure? E.g. X-axis: time, Y-axis: stock price.

Thanks a lot!

jruizaranguren
  • 12,679
  • 7
  • 55
  • 73
user3361508
  • 815
  • 3
  • 10
  • 11

4 Answers4

52

As of Bokeh 0.11.1, the user's guide section on axes now shows how to edit properties of existing axes. The way to do it is the same as before:

p = figure(width=300, height=300, x_axis_label='Initial xlabel')
p.xaxis.axis_label = 'New xlabel'
bigreddot
  • 33,642
  • 5
  • 69
  • 122
thorbjornwolf
  • 1,788
  • 18
  • 19
  • 2
    There is also quite a bit of information in the users guide now: http://bokeh.pydata.org/en/latest/docs/user_guide/styling.html#axes – bigreddot Apr 26 '16 at 19:21
8

Check out this example: elements.py

You can also now give general plot related options (plot_width, title, etc.) to a call to figure(...) instead of the renderer function (circle, in that example)

bigreddot
  • 33,642
  • 5
  • 69
  • 122
  • 1
    For completeness: if `p = figure()`, then `p.xaxis.axis_label = "foo"` sets the label of the x-axis to `foo`. – Dror Feb 27 '15 at 14:24
7

This is how you can change the axis label using CustomJS:

p = figure(x_axis_label="Initial y-axis label",
           y_axis_label="Initial x-axis label")

# ...

# p.xaxis and p.yaxis are lists. To operate on actual the axes,
# we need to extract them from the lists first.
callback = CustomJS(args=dict(xaxis=p.xaxis[0],
                              yaxis=p.yaxis[0]), code="""
    xaxis.axis_label = "Updated x-axis label";
    yaxis.axis_label = "Updated y-axis label";
""")
tuomastik
  • 4,559
  • 5
  • 36
  • 48
  • 1
    @tuomastic none of this complication (extra axis, changing visibility) is necessary to change an axis from a CustomJS. See https://discourse.bokeh.org/t/how-to-get-a-drop-down-to-change-axes-i-thought-this-would-work-but-i-dont-understand-why-not/4522/11?u=bryan – bigreddot Jan 17 '20 at 01:43
  • 2
    @bigreddot Thanks for the heads-up. I have updated the answer to include the suggested approach. It would be more intuitive for the Bokeh users if `p.xaxis` and `p.yaxis` were in the plural form: `p.xaxes` and `p.yaxes`. – tuomastik Jan 17 '20 at 06:29
4
from bokeh.plotting import figure, output_file, show
from bokeh.models.annotations import Title
p = figure(plot_width=1300, plot_height=400,x_axis_type="datetime")
p.xaxis.axis_label = 'Time'
p.yaxis.axis_label = 'Stock Price'
p.line(time,stock_price)
t = Title()
t.text = 'Stock Price during year 2018'
p.title = t
show(p)
Sharath Mohan
  • 118
  • 1
  • 3