简体   繁体   中英

Python Ploty Graph Objects - change y-axis on some subplots to percentage %

I am trying to format the y-axis of some subplots to be a percentage. I would like the y-axis on the ROIC, ROE, GM%, and CROCI subplots to be formatted as percent. I have tried to insert tickformat: ',.0%', a couple of different ways, but I can not get the syntax correct. Can some point me in the right direction? Here are the charts I am producing: 输出图表

Here is my code:

df = big_df.loc[symbol].tail(20).copy()

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

fig = make_subplots(rows=3, cols=2, 
      subplot_titles=("Revenue", "ROIC", "Shares Outstanding", "ROE", "GM %", 'CROCI'))              
fig.update_layout(height=500, width=700,
      title_text=symbol)

fig.add_trace(go.Scatter(x=df['Report Date'], y=df['revenue'], mode='lines+markers', name=symbol), row=1, col=1)
fig.add_trace(go.Scatter(x=df['Report Date'], y=df['ROIC'], mode='lines+markers'), row=1, col=2)
fig.add_trace(go.Scatter(x=df['Report Date'], y=df['shares_(diluted)'], mode='lines+markers'), row=2, col=1)
fig.add_trace(go.Scatter(x=df['Report Date'], y=df['ROE'], mode='lines+markers'), row=2, col=2)
fig.add_trace(go.Scatter(x=df['Report Date'], y=df['gross_margin'], mode='lines+markers'), row=3, col=1)
fig.add_trace(go.Scatter(x=df['Report Date'], y=df['CROCI'], mode='lines+markers'), row=3, col=2)

fig.update_layout(showlegend=False)

fig.show()

The answer is that each subplot has its own number from 1 to X (across the rows then down the columns). You can control the format of any subplot by explicitly referring to it like this:

fig.update_layout(yaxis2_tickformat = '%') #For the ROIC Chart

fig.update_layout(yaxis4_tickformat = '%') #For the ROE Chart

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