1

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()
1
  • a sample of your df might be helpful so we can reproduce your error – you can do this by copying and pasting the output from df.head(10).to_dict() into your question – but one thing that stands out to me is that in your loop, you're adding the same trace on each iteration. and i'm also not sure if [df['long'].shift(), df['long']] will be interpreted correctly by plotly as that's a list of two pd.Series Commented Dec 7, 2022 at 16:54

1 Answer 1

1

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'),
        )
    )

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, that works very well. I just note that the above code has to execute before the text which is to display hovering over a point. Otherwise "hoverinfo" does not display

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.