使用
examples
以及来自官方的数据
reference
,我已经替换了您的问题,并确定了阻止绘制线段的问题。您需要指定一列文本,指示要设置颜色的组。因此,将颜色设置为数据中的组键列。
import plotly.express as px
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.sort_values('cnt', ascending=False, inplace=True)
df_flight_paths = df_flight_paths.query("airport2 in ['DFW','LAX']")
df_flight_paths.head()
start_lat start_lon end_lat end_lon airline airport1 airport2 cnt
33 33.942536 -118.408074 32.895951 -97.0372 AA LAX DFW 914
50 29.533694 -98.469778 32.895951 -97.0372 AA SAT DFW 826
44 41.979595 -87.904464 32.895951 -97.0372 AA ORD DFW 825
20 30.194533 -97.669872 32.895951 -97.0372 AA AUS DFW 820
19 33.640444 -84.426944 32.895951 -97.0372 AA ATL DFW 580
fig = px.line_mapbox(df_flight_paths,
lat="start_lat",
lon="start_lon",
color="airport2",
hover_name="airport1",
#animation_frame="time",
center =dict(lon=-97.0,lat=38.0),
zoom=3, height=600
)
fig.update_layout(mapbox_style="stamen-terrain",
mapbox_zoom=4,
mapbox_center_lon=-97.0,
mapbox_center_lat=38.0,
margin={"r":0,"t":0,"l":0,"b":0}
)
fig.show()