【问题标题】:Change colors in 100% stacked barchart plotly python在 100% 堆叠的条形图中更改颜色 python
【发布时间】: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


    【解决方案1】:

    update_traces 中设置px.barcolor_discrete_sequence 参数而不是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"])
    

    【讨论】:

    • 非常感谢@Henry Ecker!我有一个关于 plotly 的更新,突然之间百分比没有正确绘制(每个条形图的总和没有 100%)。我会尝试这种方法。
    猜你喜欢
    • 2023-03-28
    • 2017-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-25
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    相关资源
    最近更新 更多