zendevil
zendevil

Reputation: 999

How to have individual x axis ticks labelled according to a pandas dataframe in Bokeh?

I have the following dataframe:

       Foo  Bar
A      100. 20. 
B      65.2 78. 

And I want to plot this dataframe is Bokeh, such that I have a line for Foo and a Line for Bar, and the x axis ticks are labelled A and B, and not 0 and 1. So far, I have the following:

p = figure()
p.line(df["Foo"], df.index.values)
show(p)

But this still shows the x axis ticks as integers and not as the index values A and B as expected. How to show the index values?

I tried the following as well:

p = figure(x_range=df.index.values)
p.line(df["Foo"])
show(p)

And I still don't see any lines on the graph.

Upvotes: 0

Views: 126

Answers (1)

Cameron Riddell
Cameron Riddell

Reputation: 13417

The tricky part when working with bokeh is that if you want an axis to be categorical, you need to specify it's possible values on the bokeh plot when setting up the figure.

import pandas as pd
from bokeh.plotting import figure
from bokeh.io import show

df = pd.DataFrame({"Foo":[100, 65.2], "Bar": [20, 78]}, index=["A", "B"])
print(df)
     Foo  Bar
A  100.0   20
B   65.2   78


# Tell bokeh our plot will have a categorical x-axis 
#  whose values are the index of our dataframe
p = figure(x_range=df.index.values, width=250, height=250)
p.line(x="index", y="Foo", source=df, legend_label="Foo")
p.line(x="index", y="Bar", source=df, legend_label="Bar")

p.legend.location = "bottom_right"
show(p)

enter image description here

Upvotes: 1

Related Questions