您如何从Bokeh的Pandas GroupBy中谋划?

时间:2019-07-03 09:56:47

标签: python pandas dataframe bokeh

我正在尝试使用bokeh绘制这些数据。我正在尝试折线图,但是它在源代码中显示了错误,并且如何添加悬停工具提示?

我在对数据帧进行分组后也得到了此数据。

数据:

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

错误:

expected a dict or pandas.DataFrame, got books_alloted

2 个答案:

答案 0 :(得分:2)

您是否有很旧的Bokeh版本?从任何最新版本开始,您都可以将Pandas GroupBy对象直接传递给Bokeh ColumnDataSource对象。提供GroupBy时,数据源将自动填充与group.describe方法相对应的列:

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

有关更多信息,请参见Pandas Section of Handling Categorical Data

答案 1 :(得分:1)

您可以使用

# 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)

这是解决方案: enter image description here