[英]Plot single state choropleth map in plotly/how to index geojson
[英]How to plot a Plotly Choropleth map with English local authorities using GeoJSON
我从此处的 Plotly 指南中复制了示例。 我能够复制他们的美国县地图。
现在我正在尝试制作一个类似的 Choropleth 地图,除了使用英语地方当局。 我已经从这里下载了适用于英国较低层当局的 GeoJSON。 . 这是一个巨大的分辨率并且加载速度很慢,所以我将它压缩为较低分辨率的 JSON。 我的 JSON 在这里: https ://github.com/thomasvalentine/Choropleth/blob/main/Local_Authority_Districts_(December_2021)_GB_BFC.json
这个 JSON 没有 id 属性,指导说这对于将 shapefile 与数据框链接起来很重要,所以我遍历了 JSON 并插入了与我的数据框相对应的 id。 JSON 中一个条目的结构现在如下所示:
print(Local_authorities['features'][0])
{'type': 'Feature',
'geometry': {'type': 'Polygon',
'coordinates': [[[-1.24099449299996, 54.723193914000035],
[-1.270640929999956, 54.72702718800008],
[-1.319495009999969, 54.691288599000075],
[-1.341375058999972, 54.65018898900007],
[-1.380898315999957, 54.643917068000064],
[-1.250706313999956, 54.62531901600005],
[-1.22526241099996, 54.62567539600008],
[-1.22504188299996, 54.62590810800003],
[-1.173027954999952, 54.63341869200008],
[-1.198195061999968, 54.69120971400008],
[-1.24099449299996, 54.723193914000035]]]},
'properties': {'OBJECTID': 1,
'LAD21CD': 'E06000001',
'LAD21NM': 'Hartlepool',
'LAD21NMW': ' ',
'BNG_E': 447160,
'BNG_N': 531474,
'LONG': -1.27018,
'LAT': 54.67614,
'GlobalID': '{CB7275CE-D16E-45F7-8E7D-33032FB9DF9D}',
'SHAPE_Length': 0.8998598929545726,
'SHAPE_Area': 0.013057380459647069},
'id': 'Hartlepool'}
据我所知,这似乎与情节指导中的美国县示例的结构相同。 我的虚拟数据如下所示:
LA Val
0 Hartlepool 0
1 Middlesbrough 1
2 Redcar and Cleveland 2
3 Stockton-on-Tees 3
4 Darlington 4
我从情节指导中复制了代码并修改了一些部分:
from urllib.request import urlopen
import json
# load GeoJSON file
with urlopen('file:///Users/thomasvalentine/Downloads/Local_Authority_Districts_(December_2021)_GB_BFC.json') as response:
Local_authorities = json.load(response)
la_data = []
# Iterative over JSON
for i in range(len(Local_authorities["features"])):
# Extract local authority name
la = Local_authorities["features"][i]['properties']['LAD21NM']
# Assign the local authority name to a new 'id' property for later linking to dataframe
Local_authorities["features"][i]['id'] = la
# While I'm at it, append local authority name to a list to make some dummy data to test, along with i for a value to test on map
la_data.append([la,i])
import pandas as pd
# turn dummy data into a dataframe
df = pd.DataFrame(la_data)
# update column names
df.columns = ['LA','Val']
import plotly.express as px
# make choropleth
fig = px.choropleth(df, geojson=Local_authorities, locations='LA', color='Val',
color_continuous_scale="Viridis",
range_color=(0, 12),
scope="europe",
labels={'val':'value'}
)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()
代码运行没有错误,但是当它在浏览器中打开时,它只是显示一个随机的大黄色形状:在此处输入图像描述
我部署了choroplethmap_box
而不是choropleth
并且它起作用了。 请参考以下代码:
from urllib.request import urlopen
import json
with urlopen('https://raw.githubusercontent.com/thomasvalentine/Choropleth/main/Local_Authority_Districts_(December_2021)_GB_BFC.json') as response:
Local_authorities = json.load(response)
la_data = []
# Iterative over JSON
for i in range(len(Local_authorities["features"])):
# Extract local authority name
la = Local_authorities["features"][i]['properties']['LAD21NM']
# Assign the local authority name to a new 'id' property for later linking to dataframe
Local_authorities["features"][i]['id'] = la
# While I'm at it, append local authority name to a list to make some dummy data to test, along with i for a value to test on map
la_data.append([la,i])
import pandas as pd
# turn dummy data into a dataframe
df = pd.DataFrame(la_data)
# update column names
df.columns = ['LA','Val']
fig = px.choropleth_mapbox(df,
geojson=Local_authorities,
locations='LA',
color='Val',
featureidkey="properties.LAD21NM",
color_continuous_scale="Viridis",
mapbox_style="carto-positron",
center={"lat": 55.09621, "lon": -4.0286298},
zoom=4.2,
labels={'val':'value'})
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.