繁体   English   中英

条形 plot 与 plotly 按原样分组和堆叠

[英]Bar plot with plotly grouped and stacked as is

我的输入:

    df=(pd.DataFrame({'label_color':['white','white','cyan','cyan','cyan','cyan','white','white'],
                  'label_quality':['white','white','red','green','green','red','white','white'],
'label':['foo','foo','foo','foo','foo','foo','foo','foo']}))

我尝试使用 plotly 的图 plot 看起来像这样:

在此处输入图像描述

(对不起,我在 powerpoint 中做到了)让我解释一下我想要的:“一列”将是label color ,“第二列”将是label_quality ,其中x.axisdf.index并且关联 position 列与索引顺序。 y.axis也只是 label 的名称。 colors是来自 df columns["label_colors ","label_quality"] 的值

这是我不成功的 plot:

fig = px.bar(test, x =['label_color','label_quality'], orientation='h', barmode='group', color=list(test['label_color']))
fig.show()

您呈现的数据与图表 x 轴的内容不匹配,但为了达到您想要的效果,您需要为每个数据添加值,因为您无法仅使用颜色名称绘制图表。 此外,在完成水平条形图后,我已将列列表更新为所需的 colors。 更改边框的颜色以确定它在颜色方面是空白还是白色。 根据图表的用途,甘特图可能比时间线更可取。

df['color_value'] = 1
df['quality_value'] = 1

fig = px.bar(df, y=['color_value','quality_value'],
             x=[1]*len(df),
             orientation='h',
             barmode='group',
             template='plotly_white')

fig.data[0]['marker']['color'] = df['label_color'].tolist()
fig.data[1]['marker']['color'] = df['label_quality'].tolist()
fig.update_traces(marker_line_color='rgb(8,48,107)')
fig.update_layout(showlegend=False, yaxis_title='foo', xaxis_title='')
fig.show()

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM