简体   繁体   English

Plotly Express Choropleth for Country Regions

[英]Plotly Express Choropleth for Country Regions

I have a dataframe created on a csv file about Italian Covid-19 spread all over regions.我在一个 csv 文件上创建了一个关于意大利 Covid-19 的数据框,该数据帧分布在各个地区。 I was trying to create a px.choropleth plot in which showing Total Positive values for every regions in Italy.我试图创建一个 px.choropleth 图,其中显示意大利每个地区的总正值。 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.如果我只输入 'location="Country"' 图表很好,我可以看到意大利在世界地图上有颜色。 The problems start when I try to make pyplot color my regions.当我尝试使 pyplot 为我的区域着色时,问题就开始了。 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'.由于我是 pyplot express 的新手,我阅读了一些示例,我认为我必须创建一个意大利地区名称列表,然后将其放入 'choropleth' 作为 '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.如果需要,我可以提供我正在处理的 jupyter 文件的 csv 文件。

You need to provide a geojson with Italian region borders as geojson parameter to plotly.express.choropleth, for instance this one您需要提供一个带有意大利地区边界的geojson作为geojson参数到 plotly.express.choropleth,例如这个

https://gist.githubusercontent.com/datajournalism-it/48e29e7c87dca7eb1d29/raw/2636aeef92ba0770a073424853f37690064eb0ea/regioni.geojson 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.如果你使用这个,你需要明确地传递featureidkey='properties.NOME_REG'作为 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输出图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM