【问题标题】:Create stacked bar chart using bokeh in python在 python 中使用散景创建堆积条形图
【发布时间】:2018-01-03 10:29:43
【问题描述】:

我有以下数据,这些数据捕获了设备的当前类别以及类别更改是否受到影响。

Class   Class_Change
S        yes
S        yes
G        yes
P        yes
P        yes
V        no
G        yes
V        no
V        no
V        yes
P        no

现在我想在堆积条形图中显示每个类的是/否。像下面我在excel中创建的东西。该表是每个类的是/否计数,图表是它的堆积条形图。

我试过下面的代码:

df2 = pd.DataFrame({'count': df_class.groupby(["Class","Class_Change"]).size()}).reset_index()

class = df2['Class'].tolist()
class_change = df2['Class_Change'].tolist()
count = df2['count'].tolist()

source = ColumnDataSource(data=dict(Classes = class, count=count, ClassChange = class_change,color = Viridis5))

plot = figure(x_range=tiers ,y_range=(0,max(count)), plot_height=350, plot_width = 800,title="Counts",
           toolbar_location=None, tools="")

labels = LabelSet(x = 'Class', y= 'count' , text='count', level='glyph',
                       y_offset=0 , source=source, render_mode='canvas')

plot.vbar_stack(class_change, x='class', width=0.9, color='color', source=source, legend=[value(x) for x in class_change]) 

但它给出了错误:

AttributeError: 'Figure' object has no attribute 'vbar_stack'

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

【问题讨论】:

  • 需要散景吗?
  • @COLDSPEED。是的!

标签: python bokeh stacked-chart


【解决方案1】:

散景参考文档提供了一个示例,可以针对您的情况进行修改,here

假设一个名为 source 的 ColumnDataSource 具有列 2106 和 2017,那么 >以下对 vbar_stack 的调用将创建两个堆叠的 VBar 渲染器:

p.vbar_stack(['2016', '2017'], x=10, width=0.9, color=['blue', 'red'], source=source)

vbar_stack 要求其提供的数据以特定方式结构化。我们想要堆叠的值必须在我们的列数据源中表示为列。像这样的:

那么我们可以这样写:

plot.vbar_stack(['no', 'yes'], 
                x='Class', 
                width=0.9, 
                color=['blue', 'orange'],
                line_color='white',
                legend=['No', 'Yes'],  # caps prevents calling columns
                source=source)

但是,您遇到了另一个问题。 “'Figure' object has no attribute 'vbar_stack'”告诉我们 vbar_stack 方法没有为您尝试调用此方法的图形对象定义。最可能的原因是您使用的是添加 vbar_stack 之前的 Bokeh 版本。在这种情况下,解决方案是使用 pip 或 conda 将 bokeh 更新到最新版本。

【讨论】:

    猜你喜欢
    • 2020-07-29
    • 1970-01-01
    • 2021-01-01
    • 2014-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-09
    相关资源
    最近更新 更多