python - Plotly Express 乡村地区的合唱团

标签 python choropleth

我在一个关于意大利 Covid-19 分布在各个地区的 csv 文件上创建了一个数据框。我试图创建一个 px.choropleth 图,其中显示意大利每个地区的总正值。
这是代码尝试:

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

现在我报告一些信息:“国家”是我的数据框的名称,仅填充相同的值:“意大利”。如果我只输入 'location="Country"',图表很好,我可以看到意大利在世界地图上着色。
当我尝试让 pyplot 为我的区域着色时,问题就开始了。由于我是 pyplot express 的新手,所以我阅读了一些示例,我认为我必须创建一个意大利地区名称列表,然后将其放入 'choropleth' 作为 'barmode' 的输入。
显然我错了。
那么,让它运行的程序是什么(如果有的话)?
如果需要,我可以提供我正在处理的 jupyter 文件的 csv 文件。

最佳答案

您需要提供一个带有意大利区域边界的 geojson 为 geojson plotly.express.choropleth 的参数,例如这个
https://gist.githubusercontent.com/datajournalism-it/48e29e7c87dca7eb1d29/raw/2636aeef92ba0770a073424853f37690064eb0ea/regioni.geojson
如果使用这个,需要显式传递featureidkey='properties.NOME_REG'作为 plotly.express.choropleth 的参数。
工作示例:

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

关于python - Plotly Express 乡村地区的合唱团,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60870063/

相关文章:

tooltip - 格式化 Altair 等值线图图例比例和工具提示

javascript - 将文本元素添加到路径元素的中心

python - 使用用户定义的分类方案绘制等值线图(使用 geopandas)

python - HDF5存储数据的维度

python - Ctypes 抛出 "WindowsError: [Error 193] %1 is not a valid Win32 application",但这不是 32/64 位问题

python - PyQt5:在运行时更新标签

python - 在 python 中使用四阶龙格库塔求解 3 个耦合非线性微分方程

python - 如何使用python在Elastic Search中索引路径/树?

r - 使用美国县级数据创建 Choropleth map

python - Choropleth map 不为国家着色?