Iām having a hard time getting a pretty color bar position and size for my plotly subplots. Each subplot is an overlay of scatter and line plots and a 2D histogram as the contours. I can either get no color bar or something that overlaps with the legends. How can I make it one color bar, small enough to fit under the legends on the right hand side of the canvas?
Here is my code block - I commented out my failed attempts
n_time_points = len(df_all[df_all['time-point'] < 20]['time-point'].unique())
n_rows = (n_time_points + 1) // 2 # Add 1 and floor division to get even number of rows
# Create a subplot with a 2xN layout
row_width = [1.0] * n_rows
column_width = [0.5] * 2
fig = make_subplots(
rows=n_rows,
cols=2,
subplot_titles=[f'Time-Point {i}' for i in range(n_time_points)],
row_width=row_width,
column_width=column_width
)
row, col = 1, 1 # Start with the first subplot
for time_point, data in df_all[df_all['time-point'] < 20].groupby('time-point'):
# Create your scatter plots and add them to the subplot grid
trace_scatter = go.Scatter(
x=df_com_filter[df_com_filter['time-point'] == time_point]['x'],
y=df_com_filter[df_com_filter['time-point'] == time_point]['y'],
mode='markers',
marker=dict(color='red', size=5, opacity=1.0),
name=f'CoM Time-Point {time_point}'
)
trace_shortest_path = go.Scatter(
x=df_shortest_paths[df_shortest_paths['time-point'] == time_point]['x'],
y=df_shortest_paths[df_shortest_paths['time-point'] == time_point]['y'],
mode='lines',
line=dict(color='blue', width=2),
opacity=0.5,
name=f'Shortest Path TP={time_point}'
)
trace_contour_data = go.Histogram2d(
x=df_all[df_all['time-point'] == time_point]['x'],
y=df_all[df_all['time-point'] == time_point]['y'],
colorscale='Greys', # Color scale
#showscale=False, # Show color scale
)
# colorbar_trace = go.Scatter(
# x=[],
# y=[],
# mode='markers',
# marker=dict(
# colorscale='Greys',
# colorbar=dict(
# x=1.1, # X position (1.05 places it to the right of the plot)
# y=0.2, # Y position (0.2 places it lower on the plot)
# len=0.01, # Length (0.4 makes it shorter)
# thickness=10, # Thickness (20 adjusts the width of the color bar)
# ),
# )
# )
fig.update_traces(
colorbar_len=0.1, selector=dict(type='histogram2d')
)
fig.add_trace(trace_scatter, row=row, col=col)
fig.add_trace(trace_shortest_path, row=row, col=col)
fig.add_trace(trace_contour_data, row=row, col=col)
# fig.add_trace(colorbar_trace, row=row, col=col)
fig.update_xaxes(title_text='X Axis', row=row, col=col)
fig.update_yaxes(title_text='Y Axis', row=1, col=1)
col += 1
if col > 2:
col = 1
row += 1
fig.update_layout(
title_text=canvas_title,
title_x=0.1, # Center the title horizontally
title_y=0.99, # Adjust the vertical position of the title
title_font=dict(size=24), # Set the font size
)
# Show the subplots
fig.update_layout(height=2500, width=1000)
fig.show()
Edit: I just realized that the color densities are not matching between subplots. If a common color density is not possible or hard to achieve, individual color bars is also ok as long as they can be small enough to fit near each plot.
Edit 2: When I try adding color bars as traces, I got the following (even worse, removing rest of the data)
colorbar_trace = go.Scatter(
x=[0.05], # Adjust the x-coordinate for position
y=[0.1], # Adjust the y-coordinate for position
mode='markers',
marker=dict(
colorscale='Greys',
colorbar=dict(
len=0.1
),
)
)
fig.add_trace(colorbar_trace, row=row, col=col)