微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

有没有办法在两个 lon, lat 对与 Scattergeo 之间的线上设置一个悬停文本

如何解决有没有办法在两个 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()

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?