plotly go Scattermapblox plot multiple lines in a single trace

  Kiến thức lập trình

I would like to draw many lines in a single Trace:

    # for i in range(len(transports_reduced)):
    #     fig.add_trace(
    #         go.Scattermapbox(
    #             mode='lines',
    #             lon=[transports_reduced['Quelle_lon'].iloc[i], transports_reduced['Senke_lon'].iloc[i]],
    #             lat=[transports_reduced['Quelle_lat'].iloc[i], transports_reduced['Senke_lat'].iloc[i]],
    #             line=dict(width=2, color='green'),
    #             hoverinfo='text',
    #             hovertext='Transport value: ' + str(transports_reduced['Sum LDM C3'].iloc[i])            
    #         )
    #     )

This works but produces many traces.
Then I tried this:

 # Create lists to store all lines' coordinates and hover texts
    lon_lines = []
    lat_lines = []
    hover_texts = []

    # Iterate through each row of transports_reduced
    for i in range(len(transports_reduced)):
        # Append coordinates for each line
        lon_lines.append([transports_reduced['Quelle_lon'].iloc[i], transports_reduced['Senke_lon'].iloc[i]])
        lat_lines.append([transports_reduced['Quelle_lat'].iloc[i], transports_reduced['Senke_lat'].iloc[i]])
        # Create hover text for each line
        hover_texts.append('Transport value: ' + str(transports_reduced['Sum LDM C3'].iloc[i]))

    # Add a single trace with all lines
    fig.add_trace(
        go.Scattermapbox(
            mode='lines',
            lon=lon_lines[0],  # Use lists of lists for lon and lat
            lat=lat_lines[0],
            line=dict(width=2, color='green'),
            hoverinfo='text',
            hovertext=hover_texts
        )
    )

But this just prints the first line.
I see in all the examples that when you do it in a single trace, the all the lines needs to be connected. But I have a list of individual lines which I need to print. Is this possible?

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT