将 mapbox choropleth 与 python 中的附加层和标记相结合;尝试覆盖坐标标记

Combining mapbox choropleth with additional layers and markers in python; try to overlay coordinate markers

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

最终我想做的是叠加一个散点图,无论是圆圈、符号等。一些 'Point' 坐标在彩色图块上。

我不知道有什么方法可以将 scatter_mapbox 结合起来作为等值线图的踪迹,所以我尝试更新 mapbox 并使用下面的圆形图层。

请注意,我已使用另一个 .geojson 文件的轮廓在地图上绘制轮廓(是的,这部分有效),但圆圈的叠加不起作用。 (也试过 symbol 也没用。)

自从我在计算机上创建该文件后,我正在尝试解决它是否是我的 geojson 文件。尝试在 python 和 QGIS 中创建。

我的目标:在平铺地图上添加 markers/coordinates。

这是下面的代码,应该能让你看到和我一样的东西。

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

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

情节 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. class.

的完整参考