Iām creating a choropleth map and want to remove border lines from polygons.
I tried to make the marker line transparent, but Iām still seeing the line (apparently, 1 px overlap of polygon fills).
Is there any way to fix this?
import pandas as pd
import plotly.express as px
df = pd.DataFrame({'fips': ['01001', '01002'], 'test_value': [2, 2]})
geojson = {'type': 'FeatureCollection', 'features': [{
'id': '01001',
'type': 'Feature',
'properties': {},
'geometry': {'type': 'Polygon',
'coordinates': [[
[286.0241925716400, 40.77207562584153],
[286.0244929790497, 40.77207562584153],
[286.0244929790497, 40.77230312801631],
[286.0241925716400, 40.77230312801631],
[286.0241925716400, 40.77207562584153]]]},
}, {
'id': '01002',
'type': 'Feature',
'properties': {},
'geometry': {'type': 'Polygon',
'coordinates': [[
[286.0244929790497, 40.77217562584153],
[286.0247933864594, 40.77217562584153],
[286.0247933864594, 40.77240312801631],
[286.0244929790497, 40.77240312801631],
[286.0244929790497, 40.77217562584153]]]},
}]}
fig = px.choropleth_mapbox(df, geojson=geojson, locations='fips',
color='test_value', color_continuous_scale='Turbo', range_color=(0, 12),
opacity=0.3, labels={'test_value': 'Test value'},
zoom=19.5, center={'lat': 40.77218937692893, 'lon': 286.0244929790497},
mapbox_style='light',
)
fig.update_traces(marker_line_color='rgba(255,255,255,0)', selector=dict(type='choroplethmapbox')) #make the border transparent
fig.update_layout(margin={'r': 0, 't': 0, 'l': 0, 'b': 0})
fig.show()
Result:
I also tried to make the polygons slightly smaller. It seems okay when zoomed in, but the unwanted border line appears when zoomed out.
for idx, p in enumerate(geojson["features"]):
poly = Polygon(p["geometry"]["coordinates"][0])
poly = shapely.affinity.scale(poly, 0.992, 0.992, 1.0, "center")
geojson["features"][idx]["geometry"]["coordinates"][0] = list(poly.exterior.coords)
df = pd.DataFrame({'fips': ['01001', '01002'], 'test_value': [2, 2]})
fig = px.choropleth_mapbox(df, geojson=geojson, locations='fips',
color='test_value', color_continuous_scale='Turbo', range_color=(0, 12),
opacity=0.3, labels={'test_value': 'Test value'},
zoom=17.0, center={'lat': 40.77218937692893, 'lon': 286.0244929790497},
mapbox_style='light',
)
fig.update_traces(marker_line_color='rgba(255,255,255,0)', selector=dict(type='choroplethmapbox')) #make the border transparent
fig.update_layout(margin={'r': 0, 't': 0, 'l': 0, 'b': 0})
fig.show()