如何更改Plotly subplot的起始单元格索引顺序?
实现Plotly子图索引从左下角开始的方法
在Plotly中创建子图时,默认索引起始位置是左上角,要改为从左下角开始,只需在调用make_subplots()时指定start_cell="bottom-left"参数即可。
代码示例
import plotly.graph_objects as go from plotly.subplots import make_subplots # 创建3行2列的子图,设置索引从左下角开始 fig = make_subplots( rows=3, cols=2, start_cell="bottom-left" # 核心参数,指定起始单元格位置 ) # 为每个子图添加示例数据 for row in range(1, 4): for col in range(1, 3): fig.add_trace( go.Scatter(x=[1, 2, 3], y=[4, 5, 6], name=f"子图 ({row}, {col})"), row=row, col=col ) # 调整布局并展示图表 fig.update_layout(height=600, width=800, title_text="子图索引从左下角起始") fig.show()
参数说明
start_cell的可选值为"top-left"(默认)和"bottom-left":- 默认
"top-left"时,子图的(1,1)位于左上角,行号从上到下递增; - 设置为
"bottom-left"后,子图的(1,1)位于左下角,行号从下到上递增。
- 默认
内容的提问来源于stack exchange,提问作者OmG
相关产品推荐
相关产品推荐

