Skip to content

Commit d1584a4

Browse files
Merge pull request #2215 from felixniemeyer/patch-1
adding example for multiple disconnected lines
2 parents 17af8f5 + 5792cd5 commit d1584a4

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

doc/python/lines-on-maps.md

+74
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,80 @@ fig.update_layout(
111111

112112
fig.show()
113113
```
114+
### Performance improvement: put many lines in the same trace
115+
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.
116+
117+
Use ```None``` between path coordinates to create a break in the otherwise connected paths.
118+
119+
```python
120+
import plotly.graph_objects as go
121+
import pandas as pd
122+
123+
df_airports = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_us_airport_traffic.csv')
124+
df_airports.head()
125+
126+
df_flight_paths = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_aa_flight_paths.csv')
127+
df_flight_paths.head()
128+
129+
fig = go.Figure()
130+
131+
fig.add_trace(go.Scattergeo(
132+
locationmode = 'USA-states',
133+
lon = df_airports['long'],
134+
lat = df_airports['lat'],
135+
hoverinfo = 'text',
136+
text = df_airports['airport'],
137+
mode = 'markers',
138+
marker = dict(
139+
size = 2,
140+
color = 'rgb(255, 0, 0)',
141+
line = dict(
142+
width = 3,
143+
color = 'rgba(68, 68, 68, 0)'
144+
)
145+
)))
146+
147+
flight_paths = []
148+
lons = []
149+
lats = []
150+
import numpy as np
151+
lons = np.empty(3 * len(df_flight_paths))
152+
lons[::3] = df_flight_paths['start_lon']
153+
lons[1::3] = df_flight_paths['end_lon']
154+
lons[::3] = None
155+
lats = np.empty(3 * len(df_flight_paths))
156+
lats[::3] = df_flight_paths['start_lat']
157+
lats[1::3] = df_flight_paths['end_lat']
158+
lats[::3] = None
159+
160+
fig.add_trace(
161+
go.Scattergeo(
162+
locationmode = 'USA-states',
163+
lon = lons,
164+
lat = lats,
165+
mode = 'lines',
166+
line = dict(width = 1,color = 'red'),
167+
opacity = 0.5
168+
)
169+
)
170+
171+
fig.update_layout(
172+
title_text = 'Feb. 2011 American Airline flight paths<br>(Hover for airport names)',
173+
showlegend = False,
174+
geo = go.layout.Geo(
175+
scope = 'north america',
176+
projection_type = 'azimuthal equal area',
177+
showland = True,
178+
landcolor = 'rgb(243, 243, 243)',
179+
countrycolor = 'rgb(204, 204, 204)',
180+
),
181+
height=700,
182+
)
183+
184+
fig.show()
185+
186+
```
187+
114188

115189
### London to NYC Great Circle
116190

0 commit comments

Comments
 (0)