plotly / plotly.py

The interactive graphing library for Python :sparkles: This project now includes Plotly Express!
https://plotly.com/python/
MIT License
16.4k stars 2.56k forks source link

[BUG] Exporting choropleth mapbox image results in blurry traces (even using vectorized formats such as svg/pdf) #3509

Closed Hillard28 closed 2 years ago

Hillard28 commented 2 years ago

When building and exporting Mapbox choropleths with Plotly, the outputs appear blurry, even when using a vectorized format such as svg. This effects even the traces, though I could see why this might happen with the underlying map layer. The below code should be sufficient for generating a comparison. This problem does not occur when using the choropleth function.

import plotly.express as px

df = px.data.election()
geojson = px.data.election_geojson()

fig = px.choropleth_mapbox(df, geojson=geojson, color="Bergeron",
                           locations="district", featureidkey="properties.district",
                           center={"lat": 45.5517, "lon": -73.7073},
                           mapbox_style="carto-positron", zoom=9)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.write_image("test.svg")

df = px.data.election()
geojson = px.data.election_geojson()

fig = px.choropleth(df, geojson=geojson, color="Bergeron",
                    locations="district", featureidkey="properties.district",
                    projection="mercator"
                   )
fig.update_geos(fitbounds="locations", visible=False)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.write_image("test2.svg")
nicolaskruchten commented 2 years ago

This is because Mapbox uses WebGL to do rendering, which doesn't vectorize when we export. We've done our best to document this here https://plotly.com/python/static-image-export/#vector-formats-svg-and-pdf and here https://plotly.com/python/webgl-vs-svg/#webgl-limitations-and-tradeoffs :)

Hillard28 commented 2 years ago

Ah, got it! I figured it must have been specific to Mapbox's rendering; bummer that there doesn't appear to be a way around it.

jpdonasolo commented 2 years ago

@Hillard28 I was having this same problem. The only way I found to go around it was to use directly the lower-level go.Choropleth. The plot in jupyter notebook becomes significantly larger and slower, but the final pdf image was vectorized and had really good quality