Search code examples
pythonplotly-pythoncolorbarcolor-schemechoropleth

Why do colors in my color scale for choropleth map repeat and how to fix it?


I am making a choropleth map out of a data frame containing state names and most popular car makes per each state. For some reason, when I assign the make column as a color, at some point (after GMC) the colors start repeating. How do I make them unique for each make?

(piggybacking to my own question, if someone knows how to save this choropleth map as a jpeg without using kaleido or savefig, I would really appreciate if you shared the knowledge, savefig didn't work for me and kaleido takes forever and never finishes)

db_map2 = pd.merge(db_pop_makes_states,states_list,on='STATE').reset_index(drop=True)
locations = db_map2['Code'].tolist()
values = db_map2['VEHICLE_MAKER'].tolist()
px.choropleth(locations=locations, locationmode="USA-states", color=values, scope="usa",
    labels={'locations':'State','color':'Makes'},title='NUMBER OF CUSTOMERS PER STATE')

Colors start repeating after GMC


Solution

  • You can use the color_discrete_sequence keyword argument to override the sequence of colors used. See the px.choropleth docs for more details.

    You can either use one of the builtin color sequences or make up your own.