I am working on a map showing the minimum wage across the states in the US.
My code generates a map that looks good, but the legend is unordered, which is awkward. I would like it to be in ascending order (ie $7.25-8, $8-9,...). Along with this, the legend title currently just says "color" and I would like to be able to change this to be more descriptive. How can I do this?
Here is my current code:
import plotly.express as px
fig = px.choropleth(locations=df['state'],
locationmode='USA-states',
color=df['wage_cat'],
color_discrete_map={ '$7.25-8':'#e0e6f6',
'$8-9':'#c1ccee',
'$9-10':'#a2b3e5',
'$10-11':'#8399dd',
'$11-12':'#6480d4',
'$12-13':'#4566cb',
'$13-14':'#3353b7',
'$14-15':'#2a4598',
'$15-16':'#223779',
'$17-18':'#19295A',
'No state minimum wage':'#f9fafd'}
)
fig.update_layout(
title_text='State Minimum Wage as of 1-1-24',
geo = dict(
scope='usa',
projection=go.layout.geo.Projection(type = 'albers usa'),
showlakes=True, # lakes
lakecolor='rgb(255, 255, 255)'),
)
fig.show()
You can make the
wage_catcolumn aCategoricalDtype(see documentation here), then sortdfbywage_catbefore passing it topx.choropleth. To update the legend title, you can use thelegend_title_text argumentinfig.update_layout.Something like the following:
Using some sample data, here is a fully reproducible example: