How to save a choropleth_mapbox as pure svg file?

hi,

I met a problem when saving a choropleth_mapbox as SVG file. The order “fig.write_image(“fig.svg”)” did work and gave a svg file. However, it’s not a pure SVG file, only the colorbar was drawn as a vector graphic, but the whole map was drawn as an unclear bitmap. The export engine is kaleido, and I didn’t find a keyword to solve this problem.

here is what the svg file looks like(can’t upload svg file):

and here is the export code i used:

fig.write_image(“fig1.svg”)

thanks for any reply.

hi @mao97

we’ve been having some issues with kaleido.

Can you save your svg file as a regular image (png, jpeg) and show us what it looks like?

hi @adamschroeder, very glad to have your reply.

This is how the graph looks like in the HTML file, everything works fine, all functions run normally. In the mini pic you can see that all graphs and the colorbar remain clear after zooming it in。

html_1

This is what the svg file looks like. (I can’t upload svg file so I make a jpeg screenshot of SVG here and use a zooming-in mini pic to illustrate the problem) In the mini pic, you can see that the colorbar remains clear, but the map becomes unclear, showing that “fig.write_image” command does not give the right svg file:


The png file looks just the same as the other format so I didn’t put it here due to the 5 limitation of media posting of new users. In the mini pic you can see that the colorbar and the map graph all become unclear, which is expected when zooming in a png bitmap:

This is the command to make map graph:

# drawing map box, background-color: inner distance
fig = px.choropleth_mapbox(credit_rank,geojson = Sichuan,
                           locations='adcode',featureidkey='adcode',
                           color='inner_distance',
                           color_continuous_scale="Reds",
                           #range_color=(0, 1),
                           mapbox_style="white-bg",
                           center={'lon':102.06,'lat':30.65},
                           zoom=5,
                           opacity=1,
                           labels={'value':'inner_distance'},
                           hover_name = credit_rank.index
                          )
# formating layout
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0},height=600,width=700)
fig.write_image("fig.svg")

here is the info of packages and Python:
plotly: 5.9.0
kaleido: 0.2.1
python: 3.11.4

thanks a lot, best wishes to you and your team.

hi @mao97
I spoke to my colleague.
Apparently, Mapbox is raster (webgl), so it will be pixelated no matter what we do.

hi @adamschroeder
thanks for your answer. May I ask whether there is another command or class that can export SVG Map graphics in Plotly, like some GIS software such as ArcGIS?

hi @mao97
Unfortunately, not. Mapbox traces are raster.

If you use choropleth on geo subplots, then you should be able to have a desirable svg export.