简体   繁体   中英

Plotly align two Y-axis with different values

Is it possible to align two Y-axis by two different values? I would like to align my yaxis1 at zero with my yaxis2 at 1, like in the picture

picture

import plotly.graph_objects as go
from plotly.subplots import make_subplots
import pandas as pd

df = pd.DataFrame(dict(months=['jan','feb','mar','apr','may','jun'], 
                       assets = [60,20,-25,-35,20,80], 
                       liabilities = [70,75,80,90,70,50]))

# calculate ratio
df['ratio'] = df['assets'] / df['liabilities']


fig = make_subplots(specs=[[{"secondary_y": True}]])    
fig.add_trace(go.Bar(x=df['months'], y=df['assets'], marker_color='green'))
fig.add_trace(go.Bar(x=df['months'], y=df['liabilities'], marker_color='red'))
fig.add_trace(go.Scatter(x=df['months'], y=df['ratio'], marker_color='orange'), secondary_y=True)
fig.update_yaxes(showgrid=False, secondary_y=True) 
fig.show()

To set the range of the 2nd y-axis, set the range in the layout. The manual adjustment of the second y-axis affected the scale of the first y-axis, which was corrected at the same time. If it is not the intended scale, you are responsible for correcting it.

import plotly.graph_objects as go
from plotly.subplots import make_subplots
import pandas as pd

df = pd.DataFrame(dict(months=['jan','feb','mar','apr','may','jun'], 
                       assets = [60,20,-25,-35,20,80], 
                       liabilities = [70,75,80,90,70,50]))

# calculate ratio
df['ratio'] = df['assets'] / df['liabilities']

fig = make_subplots(specs=[[{"secondary_y": True}]])    
fig.add_trace(go.Bar(x=df['months'], y=df['assets'], marker_color='green',name='assets'))
fig.add_trace(go.Bar(x=df['months'], y=df['liabilities'], marker_color='red',name='liabilities'))
fig.add_trace(go.Scatter(x=df['months'], y=df['ratio'], marker_color='orange',name='ratio'), secondary_y=True)
fig.update_yaxes(showgrid=False, secondary_y=True),
fig.update_layout(autosize=True, yaxis=dict(range=[-50,150]), yaxis2=dict(range=[0,4]))#height=600,
fig.show()

在此处输入图像描述

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