【发布时间】:2021-10-06 08:00:11
【问题描述】:
我正在尝试在 plotly 中更改堆叠 100% 条形图的颜色。
我有以下数据框和代码:
import pandas as pd
import plotly
import plotly.express as px
from plotly.offline import plot
df = pd.DataFrame([["A", "Jan", "20%"],
["A", "Fev", "10%"],
["A", "Mar", "15%"],
["A", "Apr", "10%"],
["B", "Jan", "15%"],
["B", "Fev", "15%"],
["B", "Mar", "20%"],
["B", "Apr", "10%"],
["C", "Jan", "25%"],
["C", "Fev", "25%"],
["C", "Mar", "20%"],
["C", "Apr", "30%"],
["D", "Jan", "40%"],
["D", "Fev", "50%"],
["D", "Mar", "45%"],
["D", "Apr", "50%"]],
columns=["type", "month", "percentage"])
colors = plotly.colors.qualitative.Prism
fig=px.bar(df, x='month', y='percentage',
color=df['type'], barmode ='stack',
text = df['percentage'])
fig.update_traces(textfont_size=12, marker_color = colors)
fig.update_layout(title = {'text': "Title",
'x':0.5, 'xanchor': 'center'},
title_font_size=30,
legend=dict(yanchor="bottom", y=0.0,
xanchor="right", x=1.2),
legend_font_size=16,
xaxis_title = 'Months',
yaxis_title ='%',
xaxis_tickangle=-45,
width = 1000, height = 600)
fig.show()
更改整个条的颜色,而不是更改条的每个部分的颜色。
我想要的是把原来的蓝、红、绿、紫改一下:
(这就是我在fig.update_traces 中没有marker_color = colors 时得到的结果,试图根据需要更改颜色。)
我怎样才能正确地做到这一点?
【问题讨论】:
标签: python pandas plotly bar-chart