cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

plotly express choropleth map not rendering in jupyter notebook

kll
New Contributor III

I have the following code which should render a choropleth map.

import plotly.express as px
import geopandas as gpd
 
# Example GeoJSON file with polygon geometries
geojson_file = 'example.geojson'
 
# Read GeoJSON file into GeoDataFrame
*** = gpd.read_file(geojson_file)
 
# Create Mapbox GL plot
fig = px.choropleth_mapbox(
    ***, 
    geojson=***.geometry, 
    locations=***.index, 
    color='property',
    center=dict(lat=0, lon=0), 
    zoom=1,
    mapbox_style="carto-positron"
)
 
fig.update_layout(margin=dict(l=0, r=0, t=0, b=0))
fig.show()

however, when I run the code in jupyter notebook, I get a blank output. Map doesn't render.

1 REPLY 1

Anonymous
Not applicable

@Keval Shah​ :

There could be several reasons why the choropleth map is not rendering in your Jupyter notebook. Here are a few things you could try:

  1. Check that the GeoJSON file is loaded correctly: Make sure that the GeoDataFrame has been loaded correctly by checking the output of gpd.read_file(geojson_file)
  2. Check the GeoDataFrame index: The locations argument in px.choropleth_mapbox expects a list of strings or numbers representing the index of the GeoDataFrame. Make sure that the index is set and that it is of the correct type.
  3. Check that the color column exists: Make sure that the column specified in the color argument exists in the GeoDataFrame.
  4. Check that Mapbox API token is set: In order to display the map, you need to have a valid Mapbox API token. Make sure that you have set this token using mapbox_token environment variable, or by passing it directly to px.choropleth_mapbox
  5. Check that your internet connection is working: If you are using Mapbox as a basemap, you need an internet connection to load the map tiles. Make sure that you have a stable internet connection and that there are no issues with your firewall or network settings.
  6. You can also try adding fig.show() at the end of the code to display the map in a separate window, rather than inline in the Jupyter notebook.

If none of these solutions work, you could try using a different library to render the choropleth map, such as Folium or Bokeh.

Join 100K+ Data Experts: Register Now & Grow with Us!

Excited to expand your horizons with us? Click here to Register and begin your journey to success!

Already a member? Login and join your local regional user group! If there isn’t one near you, fill out this form and we’ll create one for you to join!