简体   繁体   English

如何使用 GeoJSON 与英国地方当局一起绘制 Plotly Choropleth 地图

[英]How to plot a Plotly Choropleth map with English local authorities using GeoJSON

I have copied the example from the Plotly guidance here.我从此处的 Plotly 指南中复制了示例。 and I am able to reproduce their map of US Counties.我能够复制他们的美国县地图。

Now I am trying to produce a similar Choropleth map, except using English Local Authorities.现在我正在尝试制作一个类似的 Choropleth 地图,除了使用英语地方当局。 I have downloaded a GeoJSON for English Lower Tier Authorities from here.我已经从这里下载了适用于英国较低层当局的 GeoJSON。 . . This was huge resolution and slow to load, so I've compressed it to a lower resolution JSON.这是一个巨大的分辨率并且加载速度很慢,所以我将它压缩为较低分辨率的 JSON。 My JSON is here: https://github.com/thomasvalentine/Choropleth/blob/main/Local_Authority_Districts_(December_2021)_GB_BFC.json我的 JSON 在这里: https ://github.com/thomasvalentine/Choropleth/blob/main/Local_Authority_Districts_(December_2021)_GB_BFC.json

This JSON didn't have an id property, which the guidance says is important for linking the shapefile with the dataframe, so I have iterated over the JSON and inserted ids, corresponding to my dataframe.这个 JSON 没有 id 属性,指导说这对于将 shapefile 与数据框链接起来很重要,所以我遍历了 JSON 并插入了与我的数据框相对应的 id。 The structure of one entry in the JSON now looks like this: 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'}

From what I can tell, this appears to be the same structure as the US counties example in the plotly guidance.据我所知,这似乎与情节指导中的美国县示例的结构相同。 And my dummy data looks like this:我的虚拟数据如下所示:

    LA                   Val
0   Hartlepool           0
1   Middlesbrough        1
2   Redcar and Cleveland 2
3   Stockton-on-Tees     3
4   Darlington           4

I've copied the code from the ploty guidance and adapted some parts:我从情节指导中复制了代码并修改了一些部分:

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

The code runs without errors, but when it opens in the browser, it just displays a random big yellow shape: enter image description here代码运行没有错误,但是当它在浏览器中打开时,它只是显示一个随机的大黄色形状:在此处输入图像描述

I deployed choroplethmap_box instead of choropleth and it worked.我部署了choroplethmap_box而不是choropleth并且它起作用了。 Please refer below code:请参考以下代码:

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

And here is the Ouput:这是输出: 在此处输入图像描述

暂无
暂无

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

相关问题 Plot 单 state 等值线 map 在情节/如何索引 geojson - Plot single state choropleth map in plotly/how to index geojson 给定有关岛屿地区的 geojson 数据,如何使用 Plotly 在地区级别的等值线地图上绘制数据? - Given the geojson data about districts of an island, how to plot data on a choropleth map at a district-level using Plotly? Choropleth Map 错误(在 python 中使用 plotly)使用 ONS GeoJSON 数据 - Choropleth Map Error (using plotly in python) Using ONS GeoJSON data 如何使用Plotly使用滑块制作Choropleth贴图? - How to make a choropleth map with a slider using Plotly? 如何在 Python / Plotly choropleth plot 中放大地理 map? - How to enlarge geographic map in Python/Plotly choropleth plot? 如何使用 Plotly Choropleth ZA7F5F35426B9274117Z323 在 map 中添加 static 文本 - How to add static text in map using Plotly Choropleth Python 我正在制作带有 plotly express 的等值线图。 如何将 csv 数据框中的值与 geojson 文件中的国家/地区进行匹配? - I'm making a choropleth map with plotly express. How do I match up the values in my csv dataframe with the countries in the geojson file? Plotly:如何为等值线 map 设置彩条 position? - Plotly: How to set colorbar position for a choropleth map? Choropleth map plotly python - Choropleth map plotly python Plotly express 显示等值线 map plot 的纬度属性无效 - Plotly express shows Invalid property for latitude for choropleth map plot for python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM