1

I am drawing an airline route map and would like to have a hovertext appear along the line between an origin and destination. e.g. a curved line connecting JFK with SFO should show a hovertext at the line showing "JFK-SFO/38 flights"

bananaman
  • 39
  • 3

1 Answers1

1

I added hover text based on the sample from plotly graph gallery. The reference page can be found here. Unfortunately, setting the position of the hover text to the center of the line did not work for some reason. If you move the mouse to the airport starting point side, the text will be displayed in the desired format.

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

r-beginners
  • 31,170
  • 3
  • 14
  • 32
  • I wanted the text position to be in the center of the line, and I specified the position as latitude and longitude, but it gave me an error, so I haven't found a solution. If it helps you, please hand accept it as the correct solution. – r-beginners Jul 21 '21 at 15:33
  • thanks so much for the response. I tried this and it works fine except that I have no way to differentiate other origins that end in DEN. e.g. ORD-DEN or JFK-DEN – bananaman Jul 22 '21 at 10:02
  • Does this mean that the abbreviations of the airport names are duplicated? If my answer is helpful to you, please accept it as a correct answer by clicking the check mark. – r-beginners Jul 22 '21 at 10:21
  • In a network I will have cities that have routs coming in form many places and going out to many places. So far I have not found a way to identify the routs but only one particular city pair. DEN may have a route coming in from JFK but also one from DFW and one from SFO. I would like to visualize all three and the number of flights on each route – bananaman Jul 22 '21 at 11:40