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 3 years ago • 0 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