Search code examples
plotlygeospatiallatitude-longitudeplotly-python

How should I be drawing latitude lines using plotly


I've been trying, unsuccessfully, to draw latitude lines on a map using Plotly. I've managed to boil things down to a simple example, that I thought would draw two line segments along a single latitude line, but starting and stopping at different longitudes. The code I used within a Jupyter notebook is as follows:

from plotly.offline import iplot

# First latitude line
data_dict3 = {'type': 'scattergeo',
              'lat': [48,48],
              'lon': [-130,-60],
              'marker': {'size': 5,
                         'color': 'rgb(250, 250, 10)'
                        },
              'mode': 'lines'
             }

# Second latitude line
data_dict4 = {'type': 'scattergeo',
              'lat': [48,48],
              'lon': [-170,-100],
              'marker': {'size': 5,
                         'color': 'rgb(10, 160, 0)'
                        },
              'mode': 'lines'
             }

# Map parameters
geo_dict = {'showframe': True,
            'framewidth': 25,
            'scope': 'north america',
            'showland': True,
            'landcolor': "rgb(212, 212, 212)",
            'showocean': True,
            'oceancolor': "rgb(180, 180, 225)",
            'showlakes': True,
            'lakecolor': "rgb(180, 180, 225)",
            'subunitcolor': "rgb(0,0,0)",
            'resolution': 50}

# Layout Dict
layout_dict = {'geo': geo_dict}                

# Dicts defining figure
fig = {'data':[data_dict3, data_dict4], 
       'layout':layout_dict}

iplot(fig)

I expected to see a line along 48 deg latitude that is green at its western side and yellow at its eastern side. Possibly, it would have a third color, where the green and yellow lines overlapped. Instead, I see this:

Weird Latitude Lines

Why aren't the green & yellow lines along the same latitude?


Solution

  • I am not sure why this is happening, but it appears that the curvature of a line between two points depends on the length of the line (or the change in longitude). One possible workaround is to increase the number of points between starting and ending latitudes for each trace so that when we draw a trace between (-130,48) and (-60,48), we break this trace up into (-130,48),(-129,48),...,(-61,48), (-60,48).

    # from plotly.offline import iplot
    
    import numpy as np
    import plotly.graph_objects as go
    
    min_dist = 1
    
    # First latitude line
    lon_dict3 = np.arange(-130,-60-min_dist,min_dist)
    data_dict3 = {'type': 'scattergeo',
                  'lat': [48]*len(lon_dict3),
                  'lon': lon_dict3,
                  'marker': {'size': 5,
                             'color': 'rgb(250, 250, 10)'
                            },
                  'mode': 'lines'
                 }
    
    # Second latitude line
    lon_dict4 = np.arange(-170,-100-min_dist,1)
    data_dict4 = {'type': 'scattergeo',
                  'lat': [48]*len(lon_dict4),
                  'lon': lon_dict4,
                  'marker': {'size': 5,
                             'color': 'rgb(10, 160, 0)'
                            },
                  'mode': 'lines'
                 }
    
    # Map parameters
    geo_dict = {'showframe': True,
                'framewidth': 25,
                'scope': 'north america',
                'showland': True,
                'landcolor': "rgb(212, 212, 212)",
                'showocean': True,
                'oceancolor': "rgb(180, 180, 225)",
                'showlakes': True,
                'lakecolor': "rgb(180, 180, 225)",
                'subunitcolor': "rgb(0,0,0)",
                'resolution': 50}
    
    # Layout Dict
    layout_dict = {'geo': geo_dict}                
    
    # Dicts defining figure
    fig = go.Figure({'data':[data_dict3, data_dict4], 
           'layout':layout_dict})
    
    fig.show()
    

    enter image description here