python - 改变100%堆积条形图中的颜色python

标签 python pandas plotly bar-chart

我正在尝试更改绘图中堆叠的 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()

但我得到以下信息: Stacked barchart-wrong colored

更改整个条的颜色,而不是更改条的每个部分的颜色。

我想要的是改变原来的蓝色,红色,绿色,紫色:

Stacked barchart-original

(这就是当 fig.update_traces 中没有 marker_color = color 时我得到的结果,尝试根据需要更改颜色。)

我怎样才能正确地做到这一点?

最佳答案

设置color_discrete_sequence px.bar 的参数而不是 update_traces 中的 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)

styled plot

数据和导入(删除了字符串百分比,因为它们未正确绘制):

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"])

关于python - 改变100%堆积条形图中的颜色python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68596628/

相关文章:

python - 对数对数图中的 Matplotlib 箭头

html - Plotly Dash 不能将 div 放在同一行

python - 在 plotly 中触发音频悬停效果

python - 使用 python 正则表达式从字符串中单独提取子字符串

python - python 中的并行性无法正常工作

python - 反向树构建(有奇数个 child )

python - 将值是不同长度列表的字典转换为数据框

python - 函数的条形图 - 有条件地更改函数内条形的顺序

python - 列表内的数组

python - 同一行的不同标记颜色