Black Lives Matter. Please consider donating to Black Girls Code today.

Updating colorway/color_discrete_map after creating charts with px?

I am trying to apply a color scheme to a series of charts that were created in plotly express. They each contain several traces. I can color them using px.bar(…color_discrete_sequence = px.colors.diverging.Geyser), and I do get the Geyser colors applied to the traces in order.

However, I cannot find a way to take the figure and apply the colorscheme after the fact. Any advice on how this can be done? Example below:

xdf = pd.DataFrame({'x':list(range(10))

        ,'y': np.random.rand(10)

        ,'g' : ['A' if k<6 else 'B' for k in list(range(10)) ]

        })

f2 = px.bar(xdf, x='x',y='y',color = 'g',color_discrete_sequence=px.colors.diverging.Geyser) #WORKS

f2.show()

f = px.bar(xdf, x='x',y='y',color = 'g')

f.show()

f.update_layout(colorway = px.colors.diverging.Geyser) #DOESNT WORK

Hi @datadrinkr
I’m not sure I understand what you are trying to do.

If you’d like to update the color, make sure to use the f.update_layout() before the f.show().

What do you mean update color after creating the chart?

Hi @adamschroeder,

So basically the default colors look like this:

f = px.bar(xdf, x='x',y='y',color = 'g')

f.show()

image

But i want to use an alternative color sequence. And what I want is to apply this color scheme (or be able to toggle between a few alternatives) to a list of charts already created. But if I use update_layout, it wont take effect (btw color_discrete_sequence is not available through update_layout so i tried colorway and some other options with no success):

f.update_layout(colorway = px.colors.diverging.Geyser) #will produce the following chart
image

Only if the chart was created with the alternative color sequence will it actually show them:

f2 = px.bar(xdf, x='x',y='y',color = 'g',color_discrete_sequence=px.colors.diverging.Geyser) #WORKS
f2.show()

image

So the question is - is there a way to update charts with new color schemes after they have been created?