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

2024-06-28 18:55:21 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我使用plotly mapboxplotly.express.choropleth_mapbox来显示彩色瓷砖

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

我不知道有什么方法可以将scatter_mapbox合并为choropleth的跟踪,所以我尝试更新mapbox并使用下面的圆形层

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

因为我在计算机上创建了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()

Tags: 文件namegeojsontypepropertiesplotlymapboxcolor
1条回答
网友
1楼 · 发布于 2024-06-28 18:55:21

如果要向使用plotly express创建的地物添加更多图形,可以向原始地物添加迹线。在这种情况下,由于基础地物是地图框,我们将向最初使用px.choropleth\u地图框创建的地物添加散点_地图框

plotly District_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.类的完整引用

相关问题 更多 >