renny
renny

Reputation: 600

How do you plot from a Pandas GroupBy in Bokeh?

I am trying to plot this data using bokeh. I am trying for a line graph but it shows the error in the source and also how do i add the hover tooltip?

Also i got this data after performing group by on a dataframe.

data:

books_alloted
Friday       13893
Monday       14471
Saturday     14237
Sunday       11695
Thursday     14731
Tuesday      14900
Wednesday    16073
Name: books_alloted, dtype: int64

error:

expected a dict or pandas.DataFrame, got books_alloted

Upvotes: 3

Views: 3395

Answers (2)

bigreddot
bigreddot

Reputation: 34628

Do you have a very old version of Bokeh? As of any recent version you can pass Pandas GroupBy objects directly to Bokeh ColumnDataSource objects. When you supply a GroupBy the data source will automatically be populated with columns corresponding to the group.describe method:

from bokeh.io import show
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
from bokeh.sampledata.autompg import autompg as df

df.cyl = df.cyl.astype(str)
group = df.groupby('cyl')

source = ColumnDataSource(group)

p = figure(plot_height=350, toolbar_location=None, tools="")

p.line(x='cyl', y='mpg_mean', source=source)

show(p)

enter image description here

See the Pandas Section of Handling Categorical Data for more information.

Upvotes: 4

Albo
Albo

Reputation: 1654

You can use

# read the data
df = pd.read_csv("data.csv")

# generate the plot
p = figure(title="book plot", x_axis_label="dates", y_axis_label="alloted", x_range=df.loc[:,"books"])
p.line(df.loc[:,"books"], df.loc[:,"num"], legend="books", line_width=2) # num is the number of books alloted
show(p)

this is the solution: enter image description here

Upvotes: 1

Related Questions