plotly.py
plotly.py copied to clipboard
Save choropleth figure as static png image (Offline)
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