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

Edit Legend Label for plotly.figure_factory.create_choropleth

I created the following plot using plotly.figure_factory.create_choropleth:

I want to change the labels of the legend for the last two elements like below:

  • “< -1” ==> “Missing”
  • “-1 - 0” ==> 0

If possible, I want the hover values to be changed as specified above as well.

Is there a way to do this? I included my code below for reference.

Any help would be appreciated.

# read in data
merged_data = pd.read_csv('merged_data.csv')

# pad fips_padded_zeros columns with zeros in front
merged_data['fips_padded_zeros'] = merged_data.fips_code.apply(lambda x: str(x).zfill(5))

# colors to use for plotly plot
colorscale = ["#000000", "#FFFFFF","#F6F1F1","#EEE4E4","#E6D6D6","#DEC9C9","#D6BBBB","#CEAEAE",
              "#C6A1A1","#BE9393","#B68686","#AE7878","#A66B6B","#9E5D5D","#965050",
              "#8E4343","#863535","#7E2828", "#761A1A", "#6E0D0D", "#660000"]

# color gradient endpts
endpts = [[-1], [0]]
endpts.append(list(np.linspace(1, 90, len(colorscale) - 3)))
endpts = [endpt for elem in endpts for endpt in elem] # flatten list of lists

fips_padded_zeros = merged_data['fips_padded_zeros'].tolist()
values = merged_data['crude_opioid_mortality_rate'].tolist()

# plotly plot
fig = ff.create_choropleth(
    fips=fips_padded_zeros, values=values,
    binning_endpoints=endpts,
    colorscale=colorscale,
    show_state_data=True,
    show_hover=True, centroid_marker={'opacity': 0},
    asp=2.9, title='USA by Opioid Overdose Mortality Rate, 2016',
    legend_title='Mortality Rate (per 100,000)', scope = ['usa']
)
py.iplot(fig, filename='choropleth_full_usa')