在Bokeh中创建多系列折线图

问题描述 投票:0回答:1

您好我有以下数据:

month_id            class   count
201612              B       69
201612              G       54
201612              P       31
201612              S       42
201612              V       89
201612              Other   77
201701              B       87
201701              G       79
201701              P       96
201701              S       68
201701              V       08
201701              Other   653
201702              B       67
201702              G       82
201702              P       60
201702              S       46
201702              V       96
201702              Other   72
201703              B       94
201703              G       62
201703              P       95

现在我想说明每个班级的计数在month_id的直线图中是如何变化的。如下所示。

enter image description here

因此,x轴将具有month_id,每个类将有6行,y轴将用于计数。我的数据是df2。

我试过做以下事情:

# Create new column to make plotting easier
df2['class_date'] = df2['class'] + "-" + df2['month_id'].map(str)

# x and y axes
class_date = df2['class_date'].tolist()
count = df2['count'].tolist()

# Bokeh's mapping of column names and data lists
numlines=len(df2.class.unique())
mypalette=Spectral11[0:numlines]

plot = figure(plot_width= 800 , plot_height=350)
plot.multi_line(xs= [class_date]*numlines , ys=[count],
             line_color=mypalette,line_width= 3)
show(plot)

但是这给了我以下错误:

Bokeh Error
Cannot read property 'length' of undefined

有人可以帮我解决这个问题吗?

python bokeh linechart
1个回答
0
投票

您的数据格式对于multi_line不正确。它期望列表(或数组列表等),其中每个子列表具有每个不同行的坐标。相比之下,你正在做的是一个(平面)列表:

In [1]: lst = [1,2,3]

In [2]: lst*3
Out[2]: [1, 2, 3, 1, 2, 3, 1, 2, 3]

您的数据需要看起来像:

xs = [
    [ <list of x coords for line 1> ], 
    [ <list of x coords for line 2> ],
    ...
]

同样对于ys。但是,如果你只有6行显示,我可能会建议只调用line六次,而不是一次调用multi_line

此外,如果您对x范围使用字符串(即分类)坐标,则需要明确将x_range参数的唯一坐标列表传递给plot。分类坐标没有固有的顺序,您必须指定所需的顺序。见,例如,

https://bokeh.pydata.org/en/latest/docs/user_guide/plotting.html#categorical-axes

© www.soinside.com 2019 - 2024. All rights reserved.