How to give a colorbar for lines in Scattergeo

Hi there,

Does Scattergeo can plot a colorbar for mode = “lines+markers”?
I have a flights data shown in the world map, and I need to differentiate the lines with different color based on the flights fuel burn value. In this case, I create my own color gradient and I want to create the colorbar correspondingly. But it seems Scattergeo cannot create colorbar of lines. Can anyone give me a clue?

Thanks!

# import plotly.express as px
import plotly.graph_objects as go
import pandas as pd
import colorlover as cl
import numpy as np

data = pd.read_csv(r"C:/Users/aobo/Desktop/sample_airlines_MA_fuel_full_benchmark_ab.csv")

def create_color_gradient(start_color, end_color, num_steps):
    start_rgb = np.array(start_color)
    end_rgb = np.array(end_color)
    color_gradient = []
    for step in range(num_steps):
        fraction = step / (num_steps - 1)
        rgb = (1 - fraction) * start_rgb + fraction * end_rgb
        color_str = f'rgb({int(rgb[0])}, {int(rgb[1])}, {int(rgb[2])})'
        color_gradient.append(color_str)
    return color_gradient

start_color = (0,0,255)  
end_color = (255, 0, 0)  
num_steps = 100  

gradient = create_color_gradient(start_color, end_color, num_steps)

fig = go.Figure()

for i in range(100):
    row = data.iloc[i]
    
    lon_start = row['Dep_lon']
    lat_start = row['Dep_lat']
    
    lon_end = row['Arr_lon']
    lat_end = row['Arr_lat']
    
    fuel = row['Fuel']
    
    fig.add_trace(go.Scattergeo(
        lon = [lon_start, lon_end],
        lat = [lat_start, lat_end],
        mode = 'lines+markers',
        line = dict(width = 0.7,
                    color=gradient[i],
                    colorscale = 'Viridis',
                    showscale = True,
                   colorbar=dict(title="Fuel"),
                   )
    ))
    
fig.update_layout(
    autosize = False,
    width = 1000,
    height = 500,
    showlegend = False,
    geo = dict(
#         scope = 'north america',
        projection_type = 'natural earth',
        showland = True,
#         landcolor = 'rgb(243, 243, 243)',
#         countrycolor = 'rgb(204, 204, 204)',
    ),
)       

fig.layout.coloraxis.colorbar.x = 1.1
fig.update_coloraxes(colorscale="YlGnBu")
# fig.update_traces(contours_coloring="fill")

fig.show()

It appears that you’re trying to create a scattergeo plot with lines and markers, using a custom color gradient based on flight fuel burn values. Unfortunately, scattergeo in Plotly does not natively support colorbars for lines.

However, you can achieve your desired plot by using scattermapbox with custom line geometries and markers. Here’s an example of how you can modify your code to achieve this:

pythonCopy code

import plotly.graph_objects as go
import pandas as pd
import numpy as np

data = pd.read_csv(r"C:/Users/aobo/Desktop/sample_airlines_MA_fuel_full_benchmark_ab.csv")

def create_color_gradient(start_color, end_color, num_steps):
    start_rgb = np.array(start_color)
    end_rgb = np.array(end_color)
    color_gradient = []
    for step in range(num_steps):
        fraction = step / (num_steps - 1)
        rgb = (1 - fraction) * start_rgb + fraction * end_rgb
        color_str = f'rgb({int(rgb[0])}, {int(rgb[1])}, {int(rgb[2])})'
        color_gradient.append(color_str)
    return color_gradient

start_color = (0, 0, 255)
end_color = (255, 0, 0)
num_steps = 100

gradient = create_color_gradient(start_color, end_color, num_steps)

fig = go.Figure()

for i in range(100):
    fig.add_trace(
        go.Scattermapbox(
            lat=[data['lat'][i], data['lat'][i] + 1],  # Replace with your latitudes
            lon=[data['lon'][i], data['lon'][i] + 1],  # Replace with your longitudes
            mode='lines+markers',
            line=dict(color=gradient[i]),
            marker=dict(size=6),
        )
    )

fig.update_layout(
    mapbox=dict(
        center=dict(lat=data['lat'].mean(), lon=data['lon'].mean()),  # Adjust center as needed
        zoom=3,  # Adjust zoom level as needed
    )
)

fig.show()

Please replace 'lat' and 'lon' with the actual column names from your CSV file that contain latitude and longitude information. This modified approach uses scattermapbox to create a scatter plot with lines and markers, allowing you to apply a custom color gradient and achieve the desired visual representation.