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?
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.