首页 > 解决方案 > 将 mapbox choropleth 与 python 中的附加层和标记相结合;尝试覆盖坐标标记

问题描述

所以,我正在使用 plotly mapboxplotly.express.choropleth_mapbox来显示彩色图块。

最终,我想要做的是覆盖散点图,无论是圆圈、符号等。彩色瓷砖顶部的一些“点”坐标。

我不知道将 scatter_mapbox 组合为 choropleth 的跟踪的方法,因此我尝试更新 mapbox 并使用下面的圆形图层。

请注意,我已经使用另一个.geojson文件中的轮廓来在地图上绘制轮廓(是的,这部分有效),但是圆圈的覆盖不起作用。(也尝试了符号,也没有工作。)

试图排除它是否是我的 geojson 文件,因为我在我的计算机上创建了该文件。尝试在 python 和 QGIS 中创建。

我的目标:在平铺的地理地图上添加标记/坐标。

这是下面的代码,您应该可以看到与我相同的代码。

import pandas as pd
import plotly.express as px
point_geo = {'type': 'FeatureCollection',
 'name': 'locations',
 'features': [{'type': 'Feature',
   'properties': {'description': 'xyz', 'name': 'A'},
   'geometry': {'type': 'Point', 'coordinates': [43.58283, -79.61944]}},
  {'type': 'Feature',
   'properties': {'description': 'xyz','name': 'B '},
   'geometry': {'type': 'Point', 'coordinates': [43.58294, -79.61244]}}
             ]}
abc = pd.DataFrame({'FID':['19','18'],'MEDIAN INCOME':[60000,70000]})
def_= requests.get('https://opendata.arcgis.com/datasets/3a90af0bfd034a48a1bee2f7ff4f105a_0.geojson').json()
ghi_ = requests.get('https://opendata.arcgis.com/datasets/0e149347e0b54724996d99080bed1874_0.geojson').json()
fig3 = px.choropleth_mapbox(abc, geojson=def_, 
            featureidkey='properties.FID', locations='FID', color='MEDIAN INCOME', 
            mapbox_style="carto-positron",
            opacity=0.4,
            color_continuous_scale = px.colors.sequential.Inferno_r ,
            zoom=12, center = {"lat": 43.5887 , "lon": -79.64})

fig3.update_layout(
    mapbox = {
        'layers': [
## this part works
            {'source': ghi_,
            'type': "line", 'color': "royalblue", 'opacity':1, 'line':{'width':3} },
## this part does not work
            {'source':point_geo, 'type':'circle', 'circle':{'radius':2} }
                ]},
)

fig3.show()

标签: pythonpython-3.xmapboxplotly-python

解决方案


当您想向使用 plotly express 创建的图形添加更多图形时,您可以向该原始图形添加轨迹。在这种情况下,由于您的基本图形是一个地图框,我们将向您最初使用 px.choropleth_mapbox 创建的图形添加一个 scatter_mapbox。

一个 plotly scatter_mapbox 需要一个纬度和经度列表——我只是从你的 geojson 中提取了它,并添加了文本值,以防你想将描述用作悬停信息。

将此添加到代码底部以添加标记:

lats = [item['geometry']['coordinates'][0] for item in point_geo['features']]
lons = [item['geometry']['coordinates'][1] for item in point_geo['features']]
texts = [item['properties']['description'] for item in point_geo['features']]


fig3.add_scattermapbox(
    lat = lats,
    lon = lons,
    mode = 'markers+text',
    text = texts,
    marker_size=12,
    marker_color='rgb(235, 0, 100)'
)

这是对plotly express scatter_mapbox的完整参考。班级。


推荐阅读