【发布时间】:2021-12-14 10:46:56
【问题描述】:
我想跟进这个帖子:Plotly: How to colorcode plotly graph objects bar chart using Python?。
当使用 plotly express 并指定“颜色”时,可以正确生成图例,如 vestland 帖子中所见。
这是我的快速表达代码:
data = {'x_data': np.random.random_sample((5,)),
'y_data': ['A', 'B', 'C', 'D', 'E'],
'c_data': np.random.randint(1, 100, size=5)
}
df = pd.DataFrame(data=data)
fig = px.bar(df,
x='x_data',
y='y_data',
orientation='h',
color='c_data',
color_continuous_scale='YlOrRd'
)
fig.show()
但是在使用 go.Bar 时,图例显示不正确,如下图所示:
这是我使用图形对象的代码:
bar_trace = go.Bar(name='bar_trace',
x=df['x_data'],
y=df['y_data'],
marker={'color': df['c_data'], 'colorscale': 'YlOrRd'},
orientation='h'
)
layout = go.Layout(showlegend=True)
fig = go.FigureWidget(data=[bar_trace], layout=layout)
fig.show()
我正在学习如何使用 FigureWidget,但它似乎无法使用 plotly express,所以我必须学习如何使用图形对象进行绘图。如何将图例链接到数据,以便它像vestland 帖子中的情节表达示例一样工作。
【问题讨论】:
标签: python plotly-python