# Weird Scattermapbox lines behaviour

**URL:** <https://community.plotly.com/t/weird-scattermapbox-lines-behaviour/73821>\
**Category:** Dash Python\
**Created:** [March 23, 2023, 12:57pm UTC](https://community.plotly.com/t/weird-scattermapbox-lines-behaviour/73821 "2023-03-23T12:57:56Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![nikok](https://avatars.discourse-cdn.com/v4/letter/n/ec9cab/32.png) [@nikok](https://community.plotly.com/u/nikok)\
**Post date:** [March 23, 2023, 12:57pm UTC](https://community.plotly.com/t/weird-scattermapbox-lines-behaviour/73821/1 "2023-03-23T12:57:56Z")

</div>

![Screenshot 2023-03-23 at 13.48.55](https://us1.discourse-cdn.com/flex024/uploads/plot/original/3X/6/b/6b353e8460284e50140beda451a8af85474d072c.png)

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:

```auto
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

```

---

<div class="post-metadata">

**Author:** ![AIMPED](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/aimped/32/21758_2.png) [@AIMPED](https://community.plotly.com/u/AIMPED)\
**Post date:** [March 23, 2023, 2:27pm UTC](https://community.plotly.com/t/weird-scattermapbox-lines-behaviour/73821/2 "2023-03-23T14:27:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nikok](https://avatars.discourse-cdn.com/v4/letter/n/ec9cab/32.png) [@nikok](https://community.plotly.com/u/nikok)\
**Post date:** [March 23, 2023, 2:36pm UTC](https://community.plotly.com/t/weird-scattermapbox-lines-behaviour/73821/3 "2023-03-23T14:36:52Z")

</div>

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

 ![Screenshot 2023-03-23 at 15.36.01](https://us1.discourse-cdn.com/flex024/uploads/plot/original/3X/9/a/9a0deccf9ff23dc788269b88f3dd2f3d08520421.png)

---

<div class="post-metadata">

**Author:** ![jinnyzor](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/jinnyzor/32/21194_2.png) [@jinnyzor](https://community.plotly.com/u/jinnyzor)\
**Post date:** [March 23, 2023, 2:38pm UTC](https://community.plotly.com/t/weird-scattermapbox-lines-behaviour/73821/4 "2023-03-23T14:38:38Z")

</div>

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

What happens when you zoom in with the lines?

---

<div class="post-metadata">

**Author:** ![nikok](https://avatars.discourse-cdn.com/v4/letter/n/ec9cab/32.png) [@nikok](https://community.plotly.com/u/nikok)\
**Post date:** [March 23, 2023, 2:44pm UTC](https://community.plotly.com/t/weird-scattermapbox-lines-behaviour/73821/5 "2023-03-23T14:44:07Z")

</div>

![Screenshot 2023-03-23 at 15.41.36](https://us1.discourse-cdn.com/flex024/uploads/plot/original/3X/e/b/ebd414a6654cfa3210f70984481701a335b50508.png)

---

<div class="post-metadata">

**Author:** ![jinnyzor](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/jinnyzor/32/21194_2.png) [@jinnyzor](https://community.plotly.com/u/jinnyzor)\
**Post date:** [March 23, 2023, 2:45pm UTC](https://community.plotly.com/t/weird-scattermapbox-lines-behaviour/73821/6 "2023-03-23T14:45:59Z")

</div>

Very strange…

Might be an issue with the API for mapbox…

---

<div class="post-metadata">

**Author:** ![AIMPED](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/aimped/32/21758_2.png) [@AIMPED](https://community.plotly.com/u/AIMPED)\
**Post date:** [March 23, 2023, 2:59pm UTC](https://community.plotly.com/t/weird-scattermapbox-lines-behaviour/73821/7 "2023-03-23T14:59:30Z")

</div>

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

I hade something like this in the past:

> [@go.Figure Scatter, tonexty not in plot](https://community.plotly.com/t/go-figure-scatter-tonexty-not-in-plot/72248/4):
>
> This one was interesting. Two major things I changed: make sure, that the x- values are sorted. That was the main thing. If you zoom into the center of a chart created with your initial function you will see, that the lines are zig zagged. If plotting a go.Scatter() with mode=markers the order of the points does not matter, if mode=lines it does. order the traces for the use of tonexty change some trace names aiming for a clear legend changes to be able to use numpy arrays changes in axis t…

---

<div class="post-metadata">

**Author:** ![nikok](https://avatars.discourse-cdn.com/v4/letter/n/ec9cab/32.png) [@nikok](https://community.plotly.com/u/nikok)\
**Post date:** [March 23, 2023, 2:59pm UTC](https://community.plotly.com/t/weird-scattermapbox-lines-behaviour/73821/8 "2023-03-23T14:59:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nikok](https://avatars.discourse-cdn.com/v4/letter/n/ec9cab/32.png) [@nikok](https://community.plotly.com/u/nikok)\
**Post date:** [March 23, 2023, 3:02pm UTC](https://community.plotly.com/t/weird-scattermapbox-lines-behaviour/73821/9 "2023-03-23T15:02:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![AIMPED](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/aimped/32/21758_2.png) [@AIMPED](https://community.plotly.com/u/AIMPED)\
**Post date:** [March 23, 2023, 3:02pm UTC](https://community.plotly.com/t/weird-scattermapbox-lines-behaviour/73821/10 "2023-03-23T15:02:40Z")

</div>

Yeah, you might be right, just wanted to mention it 😉

---

<div class="post-metadata">

**Author:** ![nikok](https://avatars.discourse-cdn.com/v4/letter/n/ec9cab/32.png) [@nikok](https://community.plotly.com/u/nikok)\
**Post date:** [March 27, 2023, 8:40am UTC](https://community.plotly.com/t/weird-scattermapbox-lines-behaviour/73821/11 "2023-03-27T08:40:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nikok](https://avatars.discourse-cdn.com/v4/letter/n/ec9cab/32.png) [@nikok](https://community.plotly.com/u/nikok)\
**Post date:** [March 28, 2023, 7:06am UTC](https://community.plotly.com/t/weird-scattermapbox-lines-behaviour/73821/12 "2023-03-28T07:06:53Z")

</div>

Here’s the solution for future reference:

```auto
        #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)

```

---

<div class="post-metadata">

**Author:** ![AIMPED](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/aimped/32/21758_2.png) [@AIMPED](https://community.plotly.com/u/AIMPED)\
**Post date:** [March 28, 2023, 7:52am UTC](https://community.plotly.com/t/weird-scattermapbox-lines-behaviour/73821/13 "2023-03-28T07:52:39Z")

</div>

Thanks for sharing the solution.
