简体   繁体   中英

Plotly - Plotting traces between points

I have a set of longitude and latitude coordinates I am calculating the distances between and attempting to plot on plotly.

My data is structured into a data frame using pandas with "name", "lat", "long" and distances ("dist") between each point stored in a separate columns.

Sample data:


{'name': {0: 'Veronica Session', 1: 'Lynne Donahoo', 2: 'Debbie Hanley', 3: 'Lisandra Earls', 4: 'Sybil Leef', 5: 'Jessica Mundell', 6: 'Alexandra James', 7: 'Henry Coffell', 8: 'Rose Teich', 9: 'Eric Martinez'}, 'age (years)': {0: 10, 1: 12, 2: 14, 3: 11, 4: 6, 5: 9, 6: 11, 7: 9, 8: 15, 9: 12}, 'home longitude (degrees)': {0: 14.136215105724062, 1: -82.35073741128672, 2: -91.34452348775748, 3: -138.97669945387673, 4: 33.95932301441238, 5: -9.797522458915012, 6: 35.018161767736274, 7: -84.5876956056233, 8: 151.92765611583934, 9: -19.67088886507668}, 'home latitude (degrees)': {0: 11.463797540119938, 1: 44.40537036648498, 2: 14.928904726779818, 3: 68.95146392131102, 4: -1.6783561807047136, 5: 14.45333967686018, 6: -5.605614947366739, 7: 13.073486216442122, 8: -83.03912875968318, 9: 65.57777064215732}, 'lat': {0: 11.463797540119938, 1: 44.40537036648498, 2: 14.928904726779818, 3: 68.95146392131102, 4: -1.6783561807047136, 5: 14.45333967686018, 6: -5.605614947366739, 7: 13.073486216442122, 8: -83.03912875968318, 9: 65.57777064215732}, 'long': {0: 14.136215105724062, 1: -82.35073741128672, 2: -91.34452348775748, 3: -138.97669945387673, 4: 33.95932301441238, 5: -9.797522458915012, 6: 35.018161767736274, 7: -84.5876956056233, 8: 151.92765611583934, 9: -19.67088886507668}, 'dist': {0: nan, 1: 9625.250717024943, 2: 3385.8956180372165, 3: 6859.237699961511, 4: 12515.84742596659, 5: 5140.1303406848665, 6: 5421.465603538021, 7: 13350.896827699213, 8: 11879.820752865564, 9: 18062.08762667251}}

I can plot static points just fine but when I try to draw a trace between each point, nothing shows.

I suspect I am implementing the "shift" function wrong in this section (flight_paths = []). Help is appreciated.

I do not think the code for calculating the distances between points is relevent for this solution but I have included it anyway.

def haversine(lat1, lon1, lat2, lon2, to_radians=True, earth_radius=6371):
    if to_radians:
        lat1, lon1, lat2, lon2 = np.radians([lat1, lon1, lat2, lon2])

    a = np.sin((lat2-lat1)/2.0)**2 + \
        np.cos(lat1) * np.cos(lat2) * np.sin((lon2-lon1)/2.0)**2
    return earth_radius * 2 * np.arcsin(np.sqrt(a))

df['dist'] = haversine(df['lat'].shift(), df['long'].shift(), df['lat'], df['long'])

print(df['dist'])


fig = go.Figure()

fig.add_trace(go.Scattergeo(
    locationmode = 'ISO-3',
    lon = df['long'],
    lat = df['lat'],
    hoverinfo = 'text',
    mode = 'markers',
    text = df['name'],
    marker = dict(
        size = 2,
        color = 'rgb(255, 0, 0)',
        line = dict(
            width = 3,
            color = 'rgba(68, 68, 68, 0)'
        )
    )))

flight_paths = []
for i in range(len(df)):
    fig.add_trace(
        go.Scattergeo(
            locationmode = 'ISO-3',
            lon = [df['long'].shift(), df['long']],
            lat = [df['lat'].shift(), df['lat']],
            mode = 'lines',
            line = dict(width = 1,color = 'red'),
        )
    )

fig.update_layout(
    title_text = 'title',
    showlegend = False,
    geo = dict(
        scope = 'world',
        projection_type = 'azimuthal equal area',
        showland = True,
        landcolor = 'rgb(243, 243, 243)',
        countrycolor = 'rgb(204, 204, 204)',
    ),
)

fig.show()

I think you'll want to iterate between each sequential pair of latitude and longitude coordinates in your dataframe (see here ).

Here is the code that I added to your code:

lons = df['home longitude (degrees)']
lats = df['home latitude (degrees)']

for lon_start, lat_start, lon_end, lat_end in zip(lons, lats, lons[1:], lats[1:]):
    fig.add_trace(
        go.Scattergeo(
            locationmode = 'ISO-3',
            lon = [lon_start, lon_end],
            lat = [lat_start, lat_end],
            mode = 'lines',
            line = dict(width = 1,color = 'red'),
        )
    )

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM