Weird Scattermapbox lines behaviour

See attached screenshot. How can I fix that weird behaviour? All coords I submit are “in the water” and at 180deg lon there’s a cutoff (they are not connected). It works perfectly fine for every set of coords which “does not cross the pacific”.

Here’s the code:

def update_map_fig(datastore):
        waypoints=pd.DataFrame(datastore)
        map_fig_updated = go.Figure(go.Scattermapbox(
        mode="lines",
        lon=waypoints.lon.values, lat=waypoints.lat.values))
        map_fig_updated.update_layout( 
        mapbox=dict(accesstoken="mytoken"), margin={"r": 0, "t": 0, "l": 0, "b": 0},showlegend=False)
        return map_fig_updated

If you change the mode to ‘markers’ are the markers plotted in the correct spots?

2 Likes

good question! Yes, the markers are on the correct spot:

I think the lines could be an issue with how the map works.

What happens when you zoom in with the lines?

1 Like

Screenshot 2023-03-23 at 15.41.36

Very strange…

Might be an issue with the API for mapbox…

In a scatter plot the order of the points defines the lines to be drawn.

I hade something like this in the past:

that cutoff happens right at 180 deg long (and is obviously not visible until fully/max zoomed in)

Since this problem is exclusive to lines that go “around the world”, meaning lines that cross 180 deg lon (and I’m pretty sure the order is correct too) I don’t think that’s it

Yeah, you might be right, just wanted to mention it :wink:

So I have found out that this is in fact a feature, not a bug. Crossing the antimeridian - any tips on how to proceed?

Here’s the solution for future reference:

        #waypoints is a pandas df with columns lon and lat in degrees ranging from -180 to 180
        waypoints=pd.DataFrame(waypoints)
        diffs=np.diff(lons:=waypoints.lon.values)
        #the number 180 depends on your "step size", may need customization
        crossings_plusminus=np.where(diffs<=-180)[0]
        crossing_minusplus=np.where(diffs>180)[0]
        for plusmin_crossing in crossings_plusminus:
            lons[plusmin_crossing+1:]+=360
        for minusplus_crossing in crossing_minusplus:
            lons[minusplus_crossing+1:]-=360
        fig = px.line_mapbox(lat=waypoints.lat, lon=lons)
1 Like

Thanks for sharing the solution.