【问题标题】:How to plot a plotly scattermapbox with pandas?如何用熊猫绘制一个散点图框?
【发布时间】:2020-01-08 16:25:40
【问题描述】:

我正在尝试使用 geojson 文件在散点图框上绘制纬度/经度点。我将 geojson 文件放入 panada 的数据框中,并为 lat 和 lon 创建了一个空列表,因为这是所有 scattermapbox 接受的。结果并没有达到预期。

Python 剧情:

import plotly.graph_objects as go
import pandas as pd
from pandas.io.json import json_normalize
import json

geojson = json.load(open("Data/High Comfort Bike Lanes.geojson"))
geojson = json_normalize(geojson['features'], sep="_")

lon_comfortBike = []
lat_comfortBike = []

for l1 in geojson['geometry_coordinates']:
    for l2 in l1:
        for l3 in l2:
            lon_comfortBike.append(l2[0])
            lat_comfortBike.append(l2[1])

fig = go.Figure(
       data=[
            go.Scattermapbox(
            name='High Comfort Bike Route',
            hoverinfo='name',
            lat=lat_comfortBike,
            lon=lon_comfortBike,
            mode="lines",
            line=dict(width=3, color="#F00")
            ]
        )
    mapLegend = go.layout.Legend(
            x=0,
            y=1,
            traceorder="normal",
            font=dict(
                family="sans-serif",
                size=12,
                color="black"
            ),
            bgcolor="LightSteelBlue",
            bordercolor="Black",
            borderwidth=2
        )

fig.update_layout(
    showlegend=True,
    legend=mapLegend,
    margin={"r":0,"t":0,"l":0,"b":0},
    mapbox=go.layout.Mapbox(
        style="stamen-terrain", 
        zoom=12, 
        center_lat = 40.55,
        center_lon = -105.08,
    )
)
fig.show()

Python 数据框:

0      [[[-105.077274, 40.514625], [-105.077005, 40.5...
1      [[[-105.024284, 40.509791], [-105.024274, 40.5...
2      [[[-105.087928, 40.578187], [-105.087939, 40.5...
3      [[[-105.11976, 40.589318], [-105.11977, 40.587...
4      [[[-105.083718, 40.568761], [-105.08459, 40.56...
                             ...
995    [[[-105.05362, 40.525161], [-105.053607, 40.52...
996    [[[-105.030003, 40.62114], [-105.030012, 40.62...
997    [[[-105.123316, 40.560645], [-105.123353, 40.5...
998    [[[-105.070162, 40.580083], [-105.070175, 40.5...
999    [[[-105.120617, 40.560044], [-105.120637, 40.5...
Name: geometry_coordinates, Length: 1000, dtype: object

关于如何正确循环数据框并绘制这些线的任何建议?

【问题讨论】:

  • 你说的extract是什么意思,你能说得更具体点吗?此外,不鼓励共享屏幕截图,请仅在信息的性质意味着无法轻松共享为文本时才这样做。也可以尝试使用您的数据进行尝试。
  • 帖子已被编辑。感谢您澄清我应该解决的问题。通过提取,我的意思是如何正确地从数据框中调用纬度/经度坐标,以便我可以在 plotly 中绘制线。
  • 这不是你已经写的吗?您能否更具体一点,过程的哪一部分(您是否已将其分解?)是问题所在?
  • 问题是我有一个线串作为我的几何类型,我不知道如何在 plotly scattermapbox 中绘制这个特定的几何类型。 Plotly 只接受 lat 和 lon 坐标,而不是我的 plotly 标题下看到的它们对。我尝试在我的 panadas 数据框中创建一行 lat 和一行 lon,但这也不起作用。
  • 什么是 linestring?我仍然不确定我是否理解这个问题,抱歉。

标签: python pandas dataframe plotly-dash plotly-python


【解决方案1】:

好的。因此,将 pandas 与 plotly scattermapbox 一起使用是一件令人头疼的事情。这就是我从 geojson 文件中提取坐标的方法,这些坐标将绘制在 go.Scattermapboxlatlon 参数中。

import plotly.graph_objects as go
import json

geojson = json.load(open("Data/lanes.geojson"))

points = []

for  feature in geojson['features']:
    if feature['geometry']['type'] == 'Polygon':
        points.extend(feature['geometry']['coordinates'][0])    
        points.append([None, None]) # mark the end of a polygon   
    elif feature['geometry']['type'] == 'MultiPolygon':
        for polyg in feature['geometry']['coordinates']:
            points.extend(polyg[0])
            points.append([None, None]) #end of polygon
    elif feature['geometry']['type'] == 'MultiLineString': 
        points.extend(feature['geometry']['coordinates'])
        points.append([None, None])
    else: pass   

lons, lats = zip(*points)  

fig = go.Figure(
        go.Scattermapbox(
            name='High Comfort Bike Lane',
            hoverinfo='name',
            lat=lats,
            lon=lons,
            mode="lines",
            line=dict(width=3, color="#F00")
        ) 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 2016-08-26
    • 2017-05-28
    • 2017-05-07
    • 2021-11-25
    相关资源
    最近更新 更多