Search code examples
pythonplotlyscatter-plotcolorbar

Can you add a color bar to plotly python with an assigned variable for the colorbar like in matplotlib?


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.


Solution

  • You can use the documentation here to help you.

    import plotly.graph_objs as go
    import plotly.plotly as py
    
    import numpy as np
    
    trace1 = go.Scatter(
        y = np.random.randn(500),
        mode='markers',
        marker=dict(
            size=16,
            color = np.random.randn(500), #set color equal to a variable
            colorscale='Viridis',
            showscale=True
        )
    )
    data = [trace1]
    
    py.iplot(data, filename='scatter-plot-with-colorscale')
    

    enter image description here