Search code examples
pythonbokeh

Axis positions other than the edges?


Graphs in Bokeh have the axes along the edges. I can control which edge by adding

x_axis_location="above"|"below",
y_axis_location="left"|"right" 

to my call to bokeh.plotting.figure(). But what if I want the x axis to be along the line y=0, and the y axis to be at x=0? That seems like it should be the default (because that's how mathematicians always do it), but I can't see any way in which it is even possible.


Solution

  • It is possible to set an fixed position to your axes. In your case you want to set fixed_location = 0 for both, the x.axis (which is below) and the y-axis (which is left).

    Minimal Example

    from bokeh.plotting import show, figure, output_notebook
    output_notebook()
    p = figure(width=300,
               height=300,
               x_range=(-11,11),
               y_range=(-11,11),
              )
    p.below[0].fixed_location = 0
    p.left[0].fixed_location = 0
    show(p)
    

    Output

    Axis with fixed position