Python Plotly Graph Objects 如何将部分子图Y轴设置为百分比格式
Plotly子图指定Y轴为百分比格式的实现方法
你需要给对应位置的子图Y轴单独配置tickformat参数,推荐用指定行列的方式定位子图,不易出错,修改后的完整代码如下:
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) # 新增以下代码配置指定子图的Y轴格式 # ROIC对应1行2列 fig.update_yaxes(tickformat=',.0%', row=1, col=2) # ROE对应2行2列 fig.update_yaxes(tickformat=',.0%', row=2, col=2) # GM%对应3行1列 fig.update_yaxes(tickformat=',.0%', row=3, col=1) # CROCI对应3行2列 fig.update_yaxes(tickformat=',.0%', row=3, col=2) fig.update_layout(showlegend=False) fig.show()
如果你的指标原始值已经是百分比数值(比如15代表15%,而非0.15),把tickformat值改为,.0f%即可避免显示为1500%的问题。
内容的提问来源于stack exchange,提问作者Rockport Redfish
相关产品推荐
相关产品推荐

