user188439
user188439

Reputation: 131

Change colors in 100% stacked barchart plotly python

I'm trying to change colors of stacked 100% barchart plot in plotly.

I have the following dataframe and code:

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()

But I'm getting the following: Stacked barchart-wrong colored

Changing the colors of the whole bars, instead of change the colors for each section of the bars.

What I want is to change the blue, red, green, purple from the original one:

Stacked barchart-original

(That is what I get when there's no marker_color = colors in fig.update_traces, trying to change the colors as I want.)

How can I do it properly?

Upvotes: 2

Views: 4437

Answers (1)

Henry Ecker
Henry Ecker

Reputation: 35686

Set the color_discrete_sequence argument of px.bar instead of marker_color in update_traces:

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

Data and Imports (removed string percentages as they were not plotting correctly):

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

Upvotes: 5

Related Questions