matplotlib 不在Map上绘制或显示数据

wkyowqbh  于 2023-03-19  发布在  其他
关注(0)|答案(1)|浏览(152)

https://infra.datos.gob.ar/catalog/modernizacion/dataset/7/distribution/7.12/download/provincias.geojson获取数据
并且无法绘制Map

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

df4 = pd.read_excel('provincias.xls')

repo_url = 'https://infra.datos.gob.ar/catalog/modernizacion/dataset/7/distribution/7.12/download/provincias.geojson' #Archivo GeoJSON

ar_regions_geo = requests.get(repo_url).json()

fig = px.choropleth(data_frame=df4, 
                    geojson=ar_regions_geo, 
                    locations='provincia_id',
                    featureidkey='properties.id',  
                    color='Total', 
                    color_continuous_scale="burg", 
               )
fig.update_geos(showcountries=True, showcoastlines=True, showland=True, fitbounds="locations")

fig.show()

下图中的df4

bq8i3lrv

bq8i3lrv1#

正如我在评论中指出的,geojson几何体是Point,因此可以用它绘制px.scatter_geo()。请参阅参考资料中类似的示例。我已经将geojson转换为geopandas格式,并添加了一个虚拟的数字序列。原因是示例数据中的id与geojson中的id不相同。如果数据具有相同的id,则不需要这样做。
我认为最初的目的是用您的数据填充Map,所以我从here中获取了所需的geojson。我基于此geojson文件创建了一个新的示例数据,因为此数据的区域ID与您的不同。我使用此数据应用您的代码。结果,似乎有些区域没有显示。原因可能在geojson中。但是我能够显示基本的结构。你所需要做的就是找到与你的数据匹配的geojson。

像素散射_地理位置():

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

repo_url = 'https://infra.datos.gob.ar/catalog/modernizacion/dataset/7/distribution/7.12/download/provincias.geojson' #Archivo GeoJSON

ar_regions_geo = requests.get(repo_url).json()
geo_df = gpd.read_file(repo_url)
df5 = geo_df[['id']].copy()
df5['Total'] = pd.Series(np.random.randint(150000, 17000000, len(geo_df)))
geo_df2 = geo_df.merge(df5, on='id')

fig = px.scatter_geo(data_frame=geo_df2,
                     lat=geo_df.geometry.y,
                     lon=geo_df.geometry.x,
                     hover_name="nombre",
                     size=geo_df2['Total'],
                     color=geo_df2['Total']                     
               )
fig.update_geos(showcountries=True,
                showcoastlines=True,
                showland=True,
                fitbounds="locations")

fig.update_layout(autosize=True, height=350, margin={"r":0,"t":20,"l":0,"b":0})
fig.show()

import json
import pandas as pd
import numpy as np

with open('./argentina-with-regions_1413.geojson', encoding='utf-8') as f:
    ar_regions_geo = json.load(f)

ids, names = [],[]
for i in range(len(ar_regions_geo['features'])):
    ids.append(str(ar_regions_geo['features'][i]['properties']['density']))
    names.append(ar_regions_geo['features'][i]['properties']['name'])

df = pd.DataFrame({'provincia_id': ids, 'provincia': names, 'Total': np.random.randint(1500, 17000, len(ids))})

import plotly.express as px

fig = px.choropleth(data_frame=df, 
                    geojson=ar_regions_geo, 
                    locations='provincia_id',
                    featureidkey='properties.density',  
                    color='Total', 
                    color_continuous_scale="burg", 
               )
fig.update_geos(showcountries=True, showcoastlines=True, showland=True, fitbounds="locations")
fig.update_layout(autosize=True, height=450, margin={"r":0,"t":20,"l":0,"b":0})
fig.show()

相关问题