How to control color bar repeating in plotly bar chart?

Question:

I have created a plotted bar chart with Plotly where I noticed that colours are repeated when bars are above 10. I want to have control over this when the length of the x-axis is more than 10, pass a list of colours to be used. There are two orange, two purple, two sky blue, two blue, etc..
enter image description here

This is the code

df = common_helpers.get_dataframe_in_period(df, options)
if "All" not in options["clinic_nurse"]:
   df = df[df["nurse_name"].isin(options["clinic_nurse"])]
per = df.index_date.dt.to_period(options["groupby"]).astype(str)
count = df.pivot_table(
        index=per, columns="nurse_name", values="patient_count", aggfunc="sum"
    ).fillna(0)
    data = []
for col in count.columns:
        data.append(go.Bar(name=col, x=count.index, y=count[col]))
fig = go.Figure(data)
Asked By: Nemra Khalil

||

Answers:

By default, plotly has up to 10 colors. But you can control and set your favorite color sequency, for example, in px bar parameters by adding:

color_discrete_sequence=px.colors.qualitative.Dark24

I guess that, if you use go.bar it’s possible to use the same trick. Check out this source to find the color sequences with plotly.

https://plotly.com/python/discrete-color/

Updated: here i let you an example where i set my ideal color sequency in a horizontal bar char.

fig_bar_stack = px.bar(df2, x="MONTO", y="FECHA",
                                   color="PAGADOR", title=f"SUMA DE LA FACTURACION'", barmode='stack', text='PORCENTAJE',
                                   color_discrete_sequence=px.colors.qualitative.Dark24)
Answered By: Alexis Rangel Calvo

I’ve found the solution and did it finally! Just import plotly.io which will help you modify the main Plotly colours list and append it to whatever number of colours you want to have.

import plotly.io as pio
plotly_template = pio.templates["plotly"]
pio.templates["draft"] = plotly_template
additional_colors = ("#ff057e", "#690f54", "#bf6524", "#176b15")
basic_colors = pio.templates["draft"]["layout"]["colorway"]
pio.templates["draft"]["layout"]["colorway"] = basic_colors + additional_colors

Answered By: Nemra Khalil

Nemra Khalil, Please put the complete code

Answered By: Myriam_2189
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.