Skip to content

adding example for multiple disconnected lines #2215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 4, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions doc/python/lines-on-maps.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,74 @@ fig.update_layout(

fig.show()
```
### Performance improvement: put many lines in the same trace
For very large amounts (>1000) of lines, performance may become critcal. If you can relinquish setting individual line styles (e.g. opacity), you can put multiple paths into one trace. This makes the map render faster and reduces the script execution time and memory consumption.

Use ```None``` between path coordinates to create a break in the otherwise connected paths.

```python
import plotly.graph_objects as go
import pandas as pd

df_airports = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_us_airport_traffic.csv')
df_airports.head()

df_flight_paths = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_aa_flight_paths.csv')
df_flight_paths.head()

fig = go.Figure()

fig.add_trace(go.Scattergeo(
locationmode = 'USA-states',
lon = df_airports['long'],
lat = df_airports['lat'],
hoverinfo = 'text',
text = df_airports['airport'],
mode = 'markers',
marker = dict(
size = 2,
color = 'rgb(255, 0, 0)',
line = dict(
width = 3,
color = 'rgba(68, 68, 68, 0)'
)
)))

flight_paths = []
lons = []
lats = []
for i in range(len(df_flight_paths)):
lons += [df_flight_paths['start_lon'][i], df_flight_paths['end_lon'][i], None]
lats += [df_flight_paths['start_lat'][i], df_flight_paths['end_lat'][i], None]

fig.add_trace(
go.Scattergeo(
locationmode = 'USA-states',
lon = lons,
lat = lats,
mode = 'lines',
line = dict(width = 1,color = 'red'),
opacity = 0.5
)
)

fig.update_layout(
title_text = 'Feb. 2011 American Airline flight paths<br>(Hover for airport names)',
showlegend = False,
geo = go.layout.Geo(
scope = 'north america',
projection_type = 'azimuthal equal area',
showland = True,
landcolor = 'rgb(243, 243, 243)',
countrycolor = 'rgb(204, 204, 204)',
),
height=700,
)

fig.show()

```


### London to NYC Great Circle

Expand Down