Search code examples
pythonplotlyplotly-dashplotly-pythonplotly-express

How to update range_color in Plotly Express?


I am using plotly express and I want to display some data:

import plotly.express as px
dfx = px.data.tips()
fig = px.scatter(dfx, 
                 x='total_bill', y='tip', 
                 color='size', 
                 template='plotly_dark', 
                 range_color=[2,4])

My goal is to update the range color after it has been defined.

I tried something like this:

fig.update_layout(range_color=[3,6])

ValueError: Invalid property specified for object of type plotly.graph_objs.Layout: 'range'

but without success.

Are you aware of what I need to write in order to update the range color values?


Solution

  • To change the range of the color bar, you would change the maximum and minimum values of the color axis. This is different from the description of the graph settings, which can be found in fig.layout.

    import plotly.express as px
    dfx = px.data.tips()
    fig = px.scatter(dfx, 
                     x='total_bill', y='tip', 
                     color='size', 
                     template='plotly_dark', 
                     range_color=[2,4])
    
    fig.update_layout(coloraxis=dict(cmax=6, cmin=3))
    
    fig.show()
    

    enter image description here