✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🧬 Learn how to build RNA-Seq data apps with Python & Dash. Register for the May 20 Webinar!

Plotly Scattermapbox positive to negative longitudes

Hello! I am trying to make a scattermapbox using plotly and am running into some issues:

I am trying to plot a filled area using the following coordinates:

lats = [17,7, 13, 15, 17]
lons = [-170, 100, 136, 153, -170]

The problem I am running into is how go.Scattermapbox is displaying the lines. Instead of drawing a line connecting longitude 100 to -170 and crossing the 180 meridian, it draws the line all the way around the world (Please view the attached image). I believe this is due to my points straddling -180 degrees longitude. If I just add 360 to the 170, it is displayed correctly, however that just moves the problem to values that straddle 0 degrees longitude (prime meridian). Has anyone else encountered this? How do you fix this issue?

Hi @dm1681,
Reorder your data, and replace -170 by 190 (if we are identifying 180 with -180, then -170 gets 190).

lat = [7, 13,15,17 ]
lon= [100, 136, 153, 190]

With these data you’ll get the right plot:

fig=go.Figure(go.Scattermapbox(lat=lat, lon=lon, fill='toself'))#190 represents -170

                         lon=145 ),


Eventually you can add customdata and hovertemplate to get displayed on hover the right lon ,-170, instead of 190.

Yes, I had already come to that solution (by adding 360) and mentioned it in my post. I was not sure how to apply that solution generically. For instance, if I do it for every longitude < 0, then the same problem persists, except straddling 0 degrees longitude.

After some searching, I found that in order to determine which points needed to be mapped, I calculate relative distances between all the points and find the point that has the average distance > 180. Then if that point is negative, I add 360, if it is positive, I subtract 360.