Black Lives Matter. Please consider donating to Black Girls Code today.
Dash HoloViews is now available! Check out the docs.

Add a colorbar to scatter plot in python

Is it possible to add a color bar to a simple scatter plot? I have my data for the x and y axis, then would like to assign the color bar to an independent variable “z”. I cannot for the life of me figure this out or find any documentation to help me.

https://plot.ly/python/colorscales/ is no help for assigning a variable to the colorbar.

@spencerchad Here is an example:

import numpy as np
import plotly.graph_objs as go

z=np.random.randint(10, 35, size=20)
trace=dict(type='scatter',
          x=3+np.random.rand(20),
          y=-2+3*np.random.rand(20),
          mode='markers',
          marker=dict(color= z, 
                      colorscale='Viridis', size=14, colorbar=dict(thickness=20)))

axis_style=dict(zeroline=False, showline=True, mirror=True)
layout=dict(width=600, height=450, title='My plot',
            xaxis=axis_style,
            yaxis=axis_style,
           hovermode='closest')
fw=go.FigureWidget(data=[trace], layout=layout)
fw

plot

1 Like

Exactly what I was after. Did not intuitively realize the “color” under marker was to assign a variable. Thanks.

1 Like