Added second colorbar based off secondary value

Hello,

I’m currently trying to add a second colorbar to a plotly scatter graph. I currently have one colorbar added based on the values added to the graph, but I’ve added a colored outline to the markers based on a different value, and have been looking for a way to add an accompanying colorbar. Would there be a way to accomplish this?

Thank you,
Claire

Hi @clairew could you show us the code which leads to the plot you describe? Are you duplicating the traces with different marker sizes and colors? If so, you might be able to usedifferent colorscales. I suggest that without having actually tried it :wink:

Hello, thank you for the response.

I’ve not duplicated any traces so far, as of now I’ve just drawn a non-express Plotly scatter plot with makers that have outlines. The markers and outlines have different colors, which correspond to different input values. I am able to create a colorbar for the values of the markers themselves, but I also need to create a colorbar for the value of the marker outlines.

Thank you,
Claire

@clairew I’m not sure if you can assign a colorscale to the marker lines, I’ve never seen this.

I would actually do what I mentioned earlier, here an example:

import plotly.graph_objects as go
import numpy as np

# data
points = 10
x = np.arange(points)
y = np.random.randint(0,10,size=points)

fig = go.Figure(
    go.Scatter(
        x=x,
        y=y,
        showlegend=False,
        mode='markers',
        marker=dict(
            size=20,
            color=y, #set color equal to a variable
            colorscale='reds', # one of plotly colorscales
            showscale=True,
        )
    )
)

# values for the second color variable 
custom = np.arange(100,1000,100)

fig.add_trace(
    go.Scatter(
        x=x,
        y=y,
        showlegend=False,
        mode='markers',
        customdata=custom,
        marker=dict(
            size=16,
            color=custom, #set color equal to a 2nd variable
            colorscale='blues', # one of plotly colorscales
            showscale=True,
        )
    )
)

fig.update_layout(width=800, height=800)

# move the first colorbar to the left
fig.update_traces({"marker_colorbar_x":-0.15}, selector=0)


mrep colors colorbar

1 Like

Thank you! This does seem to work for my purposes. Thank you for the assistance.

Secondary question: the scale for the both the colorbars seems tied together, is there something in the example you provided that allows them to scale independently?

Hey @clairew I don’t understand the scaling question, unfortunately.

You could apply any colorscale or even a custom scale.

Apologies for the misunderstanding. I’d realized that I’d set the value for the color of the second trace as the first accidentally. Thank you again for the all the help in this thread! :+1:

1 Like