Black Lives Matter. Please consider donating to Campaign Zero's mission of ending police violence in America.
https://www.joincampaignzero.org

Coloring a map with number ranges?

I took the below code off of the plotly choropleth tutorial section. How would I change the legend & color coding to use number ranges. For example, instead of 0 to 12, have it color the ranges 0-3, 4-6, 9-12?

from urllib.request import urlopen
import json
with urlopen(‘https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json’) as response:
counties = json.load(response)

import pandas as pd
df = pd.read_csv(“https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv”,
dtype={“fips”: str})

import plotly.express as px

fig = px.choropleth(df, geojson=counties, locations=‘fips’, color=‘unemp’,
color_continuous_scale=“Viridis”,
range_color=(0, 12),
scope=“usa”,
labels={‘unemp’:‘unemployment rate’}
)
fig.update_layout(margin={“r”:0,“t”:0,“l”:0,“b”:0})
fig.show()

We’ve got some documentation about how to create a discrete color scale here: https://plotly.com/python/colorscales/#constructing-a-discrete-or-discontinuous-color-scale

Thank you for the quick answer!

How about making the legend look like this? Not so much the circles but the number ranges.

image

Yep, a little lower down on the same page there’s an example that shows how to customize the tick text: https://plotly.com/python/colorscales/#customizing-tick-text-on-discrete-color-bars

Ok this is great. I believe this is exactly what I need. Now I just need to make the tick vals, tick text and color scale all work together and deal with some outliers in my data.

Thx again.