I’m building a Mapbox figure with several traces that can be switched on and off via the legend. Several of the layers are outline (geojson) shapes for counties, zip codes, etc. I’m using choropleth_mapbox traces and set the marker color to blue for the outlines. I set “z” (data) to 0 for all locations. Combined with the default divergent colorscale (with “white” in the mid scale position), this gives me a trace with the county outlines and “nothing” (white with the opacity dialed down) inside the tile. This looks fine and I get nice hover text.
But in the figure legend, this layer shows up with the rainbow auto color scale as its key. How do I make the legend pick up the color set in the marker properties?
I’m using plotly 5.5.0.
Code below:
import json
import plotly.graph_objects as go
# from https://github.com/plotly/datasets/blob/master/geojson-counties-fips.json
COUNTIES_GEO = json.load(open("assets/geojson-counties-fips.json"))
COUNTIES_FEATURES = COUNTIES_GEO["features"]
COUNTIES = [x["id"] for x in COUNTIES_FEATURES]
COUNTY_HOVERS = [x["properties"]["NAME"] for x in COUNTIES_FEATURES]
LEGEND = dict(
itemclick='toggle',
itemdoubleclick=False,
font=dict(family="Noto Sans", size=14),
title=dict(
text="<b>SELECT TO VIEW</b>",
side='top',
),
orientation='v',
traceorder="reversed",
)
figure = go.Figure()
figure.add_trace(
go.Choroplethmapbox(
name="Counties",
geojson=COUNTIES_GEO,
locations=COUNTIES,
z=[0] * len(COUNTIES),
hoverinfo="text",
hovertext=COUNTY_HOVERS,
marker=dict(
line=dict(color="blue", width=1),
opacity=0.4,
),
showscale=False,
showlegend=True,
visible="legendonly",
)
)
figure["layout"].update(dict(
legend=LEGEND,
mapbox_style="open-street-map",
mapbox_zoom=3.5,
mapbox_center=dict(lon=-94.0, lat=38.0),
autosize=True,
hovermode="closest",
margin={'r': 0, 't': 0, 'l': 0, 'b': 0},
))
figure.show(config={'displayModeBar': False})