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

Horizontal bar chart, color dictionary

Hi there

I have adapted a line plot color dictionary syntax in the hope that it would work for a horizontal bar plot - but unfortunately it doesnt. Can anybody tell me how to use a dictionary to colour the bars?

I also want the order to be longest bar at the top (as the order specified here) - but it reverses it for some reason?

thanks a lot

import plotly.graph_objects as go

colordict={‘www.sat’: ‘orange’,
‘www.pr’: ‘purple’,
‘payments.p’: ‘violet’,
‘www.pro’: ‘green’,
‘www.prov’: ‘lightgreen’,
‘www.v’: ‘blue’,
‘www.c’: ‘aqua’,
‘www.a’: ‘brown’,
‘ork’: ‘goldenrod’}

fig = go.Figure(go.Bar(
x=[232,204,177,153,145,130],
y= [‘www.sat’,
‘payments.p’,
‘www.prov’,
‘www.c’,
‘www.pro’,
‘www.v’],
orientation=‘h’),color_discrete_map=colordict)

fig.show()

Hi @michaelk

The following fig definition works:

fig = go.Figure(go.Bar(
                    x=[232,204,177,153,145,130],
                    y= ['www.sat',
                        'payments.p',
                        'www.prov',
                        'www.c',
                        'www.pro',
                        'www.v'],
                    orientation='h',
                    marker_color = list(colordict.values())))

color_discrete_map is not a property for go.Bar. Hence it is sufficient to define a list of colors, instead of a dict. In the code above I extracted that list from the colordict definition.

Hi @empet
Thanks - colors work great!
I still have the widest bar at the bottom though - is there a way to reverse the order - so 232 is at the top?
thanks for your help

Hi @michaelk

Reverse yaxis as follows:

fig.update_yaxes(autorange='reversed')

thanks @empet - all good!