plotly.py icon indicating copy to clipboard operation
plotly.py copied to clipboard

Save choropleth figure as static png image (Offline)

Open gab23r opened this issue 4 years ago • 2 comments

I would like to save a px.Cholorpleth figure into a static image (in png) using kaleido but it did not make it work (see below)

I have pip install plotly-geo, but it did not solve the problem I give a geojson in argument to px.choropleth, so I don't understand why it needs to download https://cdn.plot.ly/usa_110m.json

from urllib.request import urlopen
import json
with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:
    counties = json.load(response)

import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv",
                   dtype={"fips": str})

import plotly.express as px

fig = px.choropleth(df, geojson=counties, locations='fips', color='unemp',
                           color_continuous_scale="Viridis",
                           range_color=(0, 12),
                           scope="usa",
                           labels={'unemp':'unemployment rate'}
                          )
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.write_image('image.png')
# ValueError: Transform failed with error code 525: unexpected error while fetching topojson file at https://cdn.plot.ly/usa_110m.json

gab23r avatar Sep 06 '22 13:09 gab23r

Hi @gab23r. What versions of plotly.py and plotly.js are you using? The image saves as it should for me with recent versions.

EDIT: Just noticed "offline" in the title :)

This looks to be somewhat of a duplicate of #3974. Basically that json file in the error has to come either from that CDN link, or an offline copy you can point plotlyConfig to.

AaronStiff avatar Mar 18 '23 16:03 AaronStiff

Hi - we are tidying up stale issues and PRs in Plotly's public repositories so that we can focus on things that are still important to our community. Since this one has been sitting for a while, I'm going to close it; if it is still a concern, please add a comment letting us know what recent version of our software you've checked it with so that I can reopen it and add it to our backlog. If you'd like to submit a PR, we'd be happy to prioritize a review, and if it's a request for tech support, please post in our community forum. Thank you - @gvwilson

gvwilson avatar Jul 11 '24 14:07 gvwilson