🚀 Gen 5 of the leading AI app deployment platform launches October 6. Click for the livestream.

Is it possible to use custom GeoJson on go.Choropleth not plotly Express

I have built a dash app where I am plotting German Districts. Find Repo here.

However, I need log scales (for the colors) and want to overwrite the hover box with actual numbers. I get the feeling this would be easier done with the old Plotly.

Current Visualization:

Ideally what I want to do is to have a visualization like here on a German level:

Hi @fbosler, two answers to your question :slight_smile:

  • yes, the go.Choropleth trace has geojson and featureidkey attributes which you can use as in the px function, see https://plotly.com/python-api-reference/generated/plotly.graph_objects.Choropleth.html for the API documentation
  • that said, you can also pass to px.choropleth the log of the values in the color argument of px.choropleth, and modify the hovertemplate to your taste using fig.update_traces. You can also tune the ticks of the colorbar, or just use a prefix like what is one here, corresponding to this dashboard
1 Like

Hi @Emmanuelle ! thx for the reply. Amazing dashboard btw. did you code it yourself?
How did you deploy it? For ease of use, I deployed mine via AWS/serverless. But it’s super slow and I am running into payload size restrictions now.

if the callbacks are slow you can try to write them as client-side callbacks (see https://dash.plotly.com/performance for more details). For this specific dashboard we wrote everything with client-side callbacks (and we also transformed the app into static html pages because we wanted to host it under Github pages but this is very hacky I would not recommend it).

Heya Emmanuelle,

not it seems like my provided geojson doesn’t do anything (as in a sense, that it still shows the world map). Where am I going wrong here?

## imports
from urllib.request import urlopen
import pandas as pd
import plotly.graph_objects as go

df = pd.read_csv('https://raw.githubusercontent.com/FBosler/covid19-dash-app/master/functions/data.csv')

def fetch_counties():
    URL = "https://raw.githubusercontent.com/FBosler/covid19-dash-app/master/functions/counties.json"
    with urlopen(URL) as response:
        counties = json.load(response)
    return counties

counties = fetch_counties()


fig = go.Figure(data=go.Choropleth(
    geojson=counties,
    locations = df[df['infected'] != 0]['Landkreis'],
    featureidkey="properties.NAME_3"
))

fig.show()

@Emmanuelle Could you give me a pointer how to make the Choropleth work with regions that is not the US? Would be really appreciated.
Cheers

Hi @fbosler it does not work because you did not pass which information should be color-coded. Below are two working examples, one with go.Choropleth and the other one with px.choropleth.

from urllib.request import urlopen
import pandas as pd
import numpy as np
import json
import plotly.graph_objects as go

df = pd.read_csv('https://raw.githubusercontent.com/FBosler/covid19-dash-app/master/functions/data.csv')


def fetch_counties():
    URL = "https://raw.githubusercontent.com/FBosler/covid19-dash-app/master/functions/counties.json"
    with urlopen(URL) as response:
        counties = json.load(response)
    return counties

counties = fetch_counties()

fig = go.Figure(data=go.Choropleth(
    geojson=counties,
    z=np.log10(df['infected']),
    locations = df['Landkreis'],
    featureidkey="properties.NAME_3"
))
fig.update_geos(fitbounds="locations")

fig.show()

or

import plotly.express as px
import numpy as np
fig = px.choropleth(df, geojson=counties, locations='Landkreis', 
                              color=np.log10(df['infected']),
                              featureidkey="properties.NAME_3")
fig.update_geos(fitbounds="locations")
fig.show()

Ahhh, you are amazing, really appreciate the help!

Cheers
Fabian