改变 100% 堆叠条形图中的颜色 python
Change colors in 100% stacked barchart plotly python
我正在尝试更改 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
时得到的结果,试图根据需要更改颜色。)
我怎样才能正确地做到这一点?
在 update_traces
中设置 color_discrete_sequence
argument of px.bar
而不是 marker_color
:
colors = plotly.colors.qualitative.Prism
fig = px.bar(df, x='month', y='percentage',
color=df['type'], barmode='stack',
text=df['percentage'].astype(str) + '%', # Add Percent Sign
color_discrete_sequence=colors)
# Don't forget to remove from update_traces
fig.update_traces(textfont_size=12)
数据和导入(删除了字符串百分比,因为它们绘制不正确):
import pandas as pd
import plotly
import plotly.express as px
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"])
我正在尝试更改 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
时得到的结果,试图根据需要更改颜色。)
我怎样才能正确地做到这一点?
在 update_traces
中设置 color_discrete_sequence
argument of px.bar
而不是 marker_color
:
colors = plotly.colors.qualitative.Prism
fig = px.bar(df, x='month', y='percentage',
color=df['type'], barmode='stack',
text=df['percentage'].astype(str) + '%', # Add Percent Sign
color_discrete_sequence=colors)
# Don't forget to remove from update_traces
fig.update_traces(textfont_size=12)
数据和导入(删除了字符串百分比,因为它们绘制不正确):
import pandas as pd
import plotly
import plotly.express as px
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"])