我正在创建一个使用带有散点图框的线连接坐标的地图。使用here的示例,我已经能够将每个坐标相互连接。
但是,我只想将“Key”列下具有“Destination”值的坐标连接到数据集中同一列具有“Origin”值的一个坐标。
这是我的数据:
import pandas as pd
lat_dest = [21.028321, 10.426675, 10.776390,-35.297591,21.028321]
lon_dest = [105.854022,-75.544342,106.701139,149.101268,105.854022]
locations = ['Hanoi, Vietnam','Cartagena, Colombia','Ho Chi Minh City, Vietnam','Canberra, Australia','Hanoi, Vietnam']
dest_key = ['Destination','Destination','Destination','Destination','Origin']
test_df = pd.DataFrame({
'lat': lat_dest,
'lon': lon_dest,
'name': locations,
'key':dest_key
})
这是地图:
import plotly.express as px
import plotly.graph_objects as go
fig = px.scatter_mapbox(
test_df,
lat="lat", lon="lon",
color="key",
zoom=2,
center = {"lat": orig_lat, "lon": orig_lon})
fig.update_layout(
mapbox_style="carto-positron",
margin={"r":0,"t":0,"l":0,"b":0},
showlegend=False
)
fig.add_trace(go.Scattermapbox(
mode = "lines",
lon = new_df['lon_dest'],
lat = new_df['lat_dest']
))