Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
612 views
in Technique[技术] by (71.8m points)

python - Plotly: Blank choropleth map after exporting to HTML

I'm trying to build an interactive, simple choropleth heatmap of all the countries in Asia. The plot displays well when in my Jupyter Notebook, but when I try to export it to html, it shows only blank.

The GeoJSON file that I'm using is this one, it's the first result when I google "asia geojson"

A preview of my DataFrame file can be seen here.

My code:

import pandas
import plotly.express as px
import plotly.graph_objects as go
import plotly.io as pio
import plotly

fig = px.choropleth(df, geojson=asia, locations='country', color='population', color_continuous_scale="reds", scope = 'asia', featureidkey="properties.admin", labels={'population':'Population Count'}, center={"lat": 30, "lon": 100})
fig.show()

plotly.offline.plot(fig, filename='myplot.html')

[Screenshot] How it looks on my Jupyter Notebook

[Screenshot] How it looks on my web browser

As we can see, the .html file shows nothing when opened in my web browser. I've read the documentations and the majority of users having similar problems, yet I can't seem to make mine work.

I tried to plot the sample dataset of USA Census Data from their official guide and it exports normally into .html, as expected.

How can I fix this? Any help would be appreciated :)

question from:https://stackoverflow.com/questions/65926926/plotly-blank-choropleth-map-after-exporting-to-html

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is an example code to output to html format. The geojson file you are using is associated with the data used on the official plotly website, and saved in an html file. My environment is jupyterLab 2.2.0 to check the file saving. Data for some countries is not shown, but if you replace it with your own data, it should be fine.

from urllib.request import urlopen
import json
with urlopen('https://gist.githubusercontent.com/hrbrmstr/94bdd47705d05a50f9cf/raw/0ccc6b926e1aa64448e239ac024f04e518d63954/asia.geojson') as response:
    asia = json.load(response)

import plotly.express as px
df = px.data.gapminder().query("year==2007")

fig = px.choropleth(df, geojson=asia,
                    locations='iso_alpha',
                    color='pop',
                    color_continuous_scale="reds",
                    scope = 'asia',
                    featureidkey="properties.sov_a3",
                    labels={'population':'Population Count'},
                    center={"lat": 30, "lon": 100})

fig.show()
fig.write_html("myplot.html")

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...