0
votes

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 :)

2
This may also be useful.fig.write_html("myplot.html") - r-beginners
@r-beginners Still not working, I'm suspecting it may be the dimensions that I plot my map in, rather than the plotly package itself. - unofficialDuck

2 Answers

0
votes

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

0
votes

EDIT: Try this.

I have imported plotly offline as pyo and ran your fig via pyo.plot() which outputs an html file to the dir of your notebook.

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
import plotly.offline as pyo
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()
pyo.plot(fig)