Best way to export figures from Jupyter Notebook while keeping layout?

Hi all,

I’m a beginner here, so please bare with me.

I have written code in Jupyter Notebook that creates a choropleth map. When I try to show the map with fig.show() everything is perfect, but I want to be able to send it to people so it can be opened on any computer without them having to run the actual code themselves.

I tried plotly.offline.plot(fig, filename=‘filename.html’) but the formatting changes (the whole map becomes a pale blue instead of white and it makes it harder to read.)

Is there a way to force the layout of figures to not change when doing offline plot? Or maybe is there a more efficient way to export this map that I do not know about?

Thanks in advance!

@jakefijux

Install the last plotly version (4.1.0)
pip install plotly --upgrade
and define a choropleth as an instance of go.Choroplethmapbox:

like in this example: https://plot.ly/python/mapbox-county-choropleth/

For more information on the attributes of this new type of Plotly chart print help(go.Chroplethmapbox).

I’ve just saved a choropleth generated this way as a svg file and it looks great, exactly like the choropleth generated in the jupyter notebook.

To save it import plotly io:

import plotly.io as pio
pio.write_image(fig, 'choropleth.svg')

Later edit: I realized that you are interested in a html file. To save the fig as a html file call pio.write_html():

pio.write_html(fig, 'choropleth.html')
1 Like
three reasons why you should buy plotly pro: support open source, get great support, host your plots and dashboards online