如何解决有没有办法在两个 lon, lat 对与 Scattergeo 之间的线上设置一个悬停文本
我正在绘制航空公司路线图,并希望在起点和目的地之间的线上显示一个悬停文本。例如连接 JFK 与 SFO 的曲线应在显示“JFK-SFO/38 航班”的行显示悬停文本
解决方法
我根据 plotly 图形库中的示例添加了悬停文本。可以在 here 找到参考页面。不幸的是,由于某种原因,将悬停文本的位置设置为行的中心不起作用。如果您将鼠标移动到机场起点一侧,文本将以所需的格式显示。
import plotly.graph_objects as go
import pandas as pd
df_flight_paths = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_aa_flight_paths.csv')
df_flight_paths.head()
start_lat start_lon end_lat end_lon airline airport1 airport2 cnt
0 32.895951 -97.037200 35.040222 -106.609194 AA DFW ABQ 444
1 41.979595 -87.904464 30.194533 -97.669872 AA ORD AUS 166
2 32.895951 -97.037200 41.938874 -72.683228 AA DFW BDL 162
3 18.439417 -66.001833 41.938874 -72.683228 AA SJU BDL 56
4 32.895951 -97.037200 33.562943 -86.753550 AA DFW BHM 168
fig = go.Figure()
flight_paths = []
for i in range(len(df_flight_paths)):
fig.add_trace(
go.Scattergeo(
locationmode = 'USA-states',lon = [df_flight_paths['start_lon'][i],df_flight_paths['end_lon'][i]],lat = [df_flight_paths['start_lat'][i],df_flight_paths['end_lat'][i]],mode = 'lines',line = dict(width = 1,color = 'red'),hoverinfo='text',text=df_flight_paths.loc[i,'airport1'] + '-' + df_flight_paths.loc[i,'airport2'] +'/' + str(df_flight_paths.loc[i,'cnt']) + 'flights',textposition='top center',opacity = float(df_flight_paths['cnt'][i]) / float(df_flight_paths['cnt'].max()),)
)
fig.update_layout(
title_text = 'Feb. 2011 American Airline flight paths<br>(Hover for airport names)',showlegend = False,width=1000,height=800,geo = dict(
scope = 'north america',projection_type = 'azimuthal equal area',showland = True,landcolor = 'rgb(243,243,243)',countrycolor = 'rgb(204,204,204)',),)
fig.show()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。