【问题标题】:Creating multi-series line chart in Bokeh在 Bokeh 中创建多系列折线图
【发布时间】:2017-12-27 09:39:14
【问题描述】:

您好,我有以下数据:

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 中变化。如下所示。

因此,x 轴将包含月份 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】:

    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 行要显示,我可能建议您只调用 6 次 line,而不是 1 次调用 multi_line。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-13
      相关资源
      最近更新 更多