Two or three colorbars for one plot in Plotly

Viewed 1879

I have a Scattergl plot and I want it to have two or three colorbars aside. Each colorbar has a different colorscale related to a different data feature. The points in the plot are colored as a combination of the two or three colorscale colors. I already have the combination of the colors part, but I have no idea of how to plot the colorbars... and I'm not sure if it's allowed to plot more than one. Do you have any idea of how to do this?

I'll appreciate your help! Thanks in advance. Kind regards.

2 Answers

Nice to see more sansanos using plotly, there is an attribute inside marker's colorbar for this purpose, trace.marker.colorbar.x, it requires a value that ranges from -2 to 3, with 0 being the left limit of the plot and 1 being the right limit, you'll usually have to get it by trial and error.

Remember that it's always usefull to provide a sample of your code, so we can provide corrections that follow the paradigm you're working on. Here's an example

from plotly import graph_objects as go

data_x = [1, 2, 3, 4, 5, 6]
data_y = [1, 4, 9, 16, 25, 36]

figure = go.FigureWidget()

# First
first_scatter = go.Scattergl()

first_scatter.mode = 'markers'
first_scatter.x = data_x
first_scatter.y = data_y

first_scatter.marker.colorscale = 'Viridis'
first_scatter.marker.color = data_y
first_scatter.marker.size = 20
first_scatter.marker.showscale = True

first_scatter.showlegend = False
figure.add_trace(first_scatter)

# Second
second_scatter = go.Scattergl()

second_scatter.mode = 'markers'
second_scatter.x = data_x
second_scatter.y = data_y[::-1]

second_scatter.marker.colorscale = 'Electric'
second_scatter.marker.color = data_y
second_scatter.marker.size = 20
second_scatter.marker.showscale = True
second_scatter.marker.colorbar.x = 1.15 # Here

second_scatter.showlegend = False
figure.add_trace(second_scatter)

example

To access the layout of the plotly. It is handy to override the attribute of the plot through fig.layout

For moving colorbar purpose: In order to separate the 2 colorbars, after creating the first trace/figure, we can access its current reference position and override with new_value before adding another trace

fig.layout.coloraxis.colorbar.x = 1.5 => this will move the bar to the right (default is 1)

fig.layout.coloraxis.colorbar.x = -0.5 => this will move the bar to the left

the same idea will also apply to y value as well as colorscale.

For friendlier using purpose, you can use update method instead:

fig.update_coloraxes(colorbar_x=<VALUE>)

For further detail, you can check out here

Here is an example, where I change the colorbar and its color reference.

#creating a figure and add the first trace

fig = px.density_contour(df[(df["label"]=='capture')&(df["bie"]=='bie')], x="x", y="y") 

# change reference params

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

# add new traces with new params

fig.add_traces(
    list(px.scatter(df[df["label"]=='capture'], x="x", y="y",color="state", opacity=0.8).select_traces()))
fig.update_layout(width=1000,height=500,legend=dict(
    yanchor="bottom"))
fig.show()

enter image description here

Related