Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly Express Choropleth for Country Regions

I have a dataframe created on a csv file about Italian Covid-19 spread all over regions. I was trying to create a px.choropleth plot in which showing Total Positive values for every regions in Italy. This the code tried:

italy_regions=[i for i in region['Region'].unique()]
fig = px.choropleth(italy_last, locations="Country",
                    locationmode=italy_regions,
                    color=np.log(italy_last["TotalPositive"]), 
                    hover_name="Region", hover_data=['TotalPositive'],
                    color_continuous_scale="Sunsetdark", 
                    title='Regions with Positive Cases')
fig.update(layout_coloraxis_showscale=False)
fig.show()

Now I report some info: 'Country' is the name given to my dataframe and is filled only with the same values: 'Italy'. If I only input 'location="Country"' the graph is fine and I can see Italy colored into the world map. The problems start when I try to make pyplot color my regions. As I'm a newbye in pyplot express, I read some examples and I thought I had to create a list of italian regions names and then put into 'choropleth' as input for 'barmode'. Clearly I'm wrong. So, what is the procedure to follow to make it run (if any)? In case of need, I can provide both the csv file that the jupyter file I'm working on.

like image 454
Polar Avatar asked Mar 02 '23 15:03

Polar


1 Answers

You need to provide a geojson with Italian region borders as geojson parameter to plotly.express.choropleth, for instance this one

https://gist.githubusercontent.com/datajournalism-it/48e29e7c87dca7eb1d29/raw/2636aeef92ba0770a073424853f37690064eb0ea/regioni.geojson

If you use this one, you need to explicitly pass featureidkey='properties.NOME_REG' as a parameter of plotly.express.choropleth.

Working example:

import pandas as pd
import requests
import plotly.express as px

regions = ['Piemonte', 'Trentino-Alto Adige', 'Lombardia', 'Puglia', 'Basilicata', 
           'Friuli Venezia Giulia', 'Liguria', "Valle d'Aosta", 'Emilia-Romagna',
           'Molise', 'Lazio', 'Veneto', 'Sardegna', 'Sicilia', 'Abruzzo',
           'Calabria', 'Toscana', 'Umbria', 'Campania', 'Marche']

# Create a dataframe with the region names
df = pd.DataFrame(regions, columns=['NOME_REG'])
# For demonstration, create a column with the length of the region's name
df['name_length'] = df['NOME_REG'].str.len()

# Read the geojson data with Italy's regional borders from github
repo_url = 'https://gist.githubusercontent.com/datajournalism-it/48e29e7c87dca7eb1d29/raw/2636aeef92ba0770a073424853f37690064eb0ea/regioni.geojson'
italy_regions_geo = requests.get(repo_url).json()

# Choropleth representing the length of region names
fig = px.choropleth(data_frame=df, 
                    geojson=italy_regions_geo, 
                    locations='NOME_REG', # name of dataframe column
                    featureidkey='properties.NOME_REG',  # path to field in GeoJSON feature object with which to match the values passed in to locations
                    color='name_length',
                    color_continuous_scale="Magma",
                    scope="europe",
                   )
fig.update_geos(showcountries=False, showcoastlines=False, showland=False, fitbounds="locations")
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()

Output image

like image 167
qasar Avatar answered Mar 12 '23 18:03

qasar