简体   繁体   中英

Change single axis in plotly subplots

I generated a plot in plotly using plotly.express. I used a pandas.DataFrame and one column to differentiate the two subplots putting it into facet_row Now my data has very different scales they are operating one, but plotly assigns the same range to both yaxis. I've tried to assign a 'range' attribute to the .layout.yaxis1 dictionary (using a list), but this changes the yaxis for both the upper and the lower plot. Minimal working example:

import plotly.express as px    
px.bar(pd.DataFrame({'x':[50,60,45,.80],'y': [800,900,1,2],"dif":['a','a','b','b']}),x='x',y='y',facet_row='dif')

How can I change the first axis alone?

If you add .update_yaxes(matches=None) that will break the linkage between the Y-axis ranges.

This works because px.bar() (and any other px.*() ) returns a Figure object which are are updatable via various .update_*() methods. These methods mutate and return the figure. px.bar(facet_row="whatever") by default sets the yxaxis*.matches attribute to y1 and so clearing that via .update_yaxes() will get the behaviour you want.

After that you can set the range independently however you like.

More information here: https://plot.ly/python/creating-and-updating-figures/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM