Search code examples
plotlyplotly-python

How to only change the range of yaxis for the second plot in px.bar and focet_row has a value?


I have this code below, and I want to change only the yaxis range of the second plot. the problem is that all yaxes' ranges changed:

import plotly.express as px

df = px.data.tips()

fig = px.bar(df, x="sex", y="total_bill", facet_row="day", barmode="group")

fig.update_yaxes(range=[0, 60], row=1, col=1)

fig.show()

I tried multiple solutions with fig.for_each_yaxis() and fig.layout["yaxis2"].update(range=[0,10]) but no solution works.

Any help is much appreciated!

enter image description here


Solution

  • From the graph layout information, there is a setting for which y-axis criteria should be adjusted to which y-axis, and this is accomplished by disabling it. I was made aware of this feature by your question.+1

    import plotly.express as px
    
    df = px.data.tips()
    
    fig = px.bar(df, x="sex", y="total_bill", facet_row="day", barmode="group")
    
    
    fig.layout["yaxis2"].update(range=[0,10], autorange=False, matches=None)
    
    fig.show()
    

    enter image description here