[英]Create stacked bar chart using bokeh in python
I have the below data which captures the current class of a device and whether a class change has been affected or not. 我有以下数据,这些数据捕获了设备的当前类别以及类别更改是否受到影响。
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
Now I want to display the Yes/No across each class in a stacked bar chart. 现在,我想在堆叠的条形图中显示每个班级的是/否。 Something like below which I have created in excel. 下面是我在excel中创建的内容。 The table is a count of yes/no across each class and the chart is the stacked bar chart of it. 该表是每个类别中是/否的计数,而图表是其堆积的条形图。
I have tried the below code: 我尝试了以下代码:
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])
but it is giving the error: 但是它给出了错误:
AttributeError: 'Figure' object has no attribute 'vbar_stack'
Can someone please help me with this?? 有人可以帮我吗?
The bokeh reference docs gives an example that can be reworked for your case, here : bokeh参考文档提供了一个可以根据您的情况进行修改的示例, 在这里 :
Assuming a ColumnDataSource named source with columns 2106 and 2017, then the >following call to vbar_stack will will create two VBar renderers that stack: 假设一个名为source的ColumnDataSource带有2106和2017列,那么> vbar_stack的以下调用将创建两个VBar渲染器,这些渲染器堆叠:
p.vbar_stack(['2016', '2017'], x=10, width=0.9, color=['blue', 'red'], source=source)
vbar_stack
requires that the data it is given is structured in a specific way. vbar_stack
要求以给定的数据结构以特定的方式构造。 The values that we want stacked must be represented as columns in our column data source. 我们要堆叠的值必须在列数据源中表示为列。 Something like this: 像这样:
Then we could write something like this: 然后我们可以这样写:
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)
However, you are running into another issue. 但是,您遇到了另一个问题。 "'Figure' object has no attribute 'vbar_stack'" tells us that the vbar_stack
method is not defined for the figure object that you are trying to call this method on. “'Figure'对象没有属性'vbar_stack'”,告诉我们没有为要调用该方法的Figure对象定义vbar_stack
方法。 The most likely cause is that you are using a version of Bokeh from before vbar_stack
was added. 最可能的原因是您在添加vbar_stack
之前使用的是Bokeh版本。 In that case the solution is to update bokeh to the latest version with either pip or conda. 在这种情况下,解决方案是使用pip或conda将bokeh更新为最新版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.