对所有子图设置 showgrid = False

2024-03-17

我有 10 多个子图,我想禁用它们的网格线showgrid=False。我需要对 x 轴和 y 轴执行此操作。

这个答案 https://stackoverflow.com/questions/63213050/plotly-how-to-set-xticks-for-all-subplots似乎很接近,但我无法扩展它来做我想做的事。

在上面的答案中,他们展示了如何设置所有子图的范围:

myRange=[0,6]
for ax in fig['layout']:
    if ax[:5]=='xaxis':
        fig['layout'][ax]['range']=myRange

但是当我尝试这样做时showgrid我收到一个错误TypeError: 'NoneType' object does not support item assignment

I tried:

for ax in fig['layout']:
    fig['layout'][ax]['showgrid'] = False

仅使用update_layout仅更改第一个子图:

fig.update_layout(
     xaxis=dict(showgrid=False), 
     yaxis=dict(showgrid=False)
)

您自己的建议有效,但对于任意数量的子图,以下建议更加灵活:

fig.for_each_xaxis(lambda x: x.update(showgrid=False))
fig.for_each_yaxis(lambda x: x.update(showgrid=False))

完整代码:

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

fig = make_subplots(rows=2, cols=2, start_cell="bottom-left")

fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6]),
              row=1, col=1)

fig.add_trace(go.Scatter(x=[20, 30, 40], y=[50, 60, 70]),
              row=1, col=2)

fig.add_trace(go.Scatter(x=[300, 400, 500], y=[600, 700, 800]),
              row=2, col=1)

fig.add_trace(go.Scatter(x=[4000, 5000, 6000], y=[7000, 8000, 9000]),
              row=2, col=2)

fig.for_each_xaxis(lambda x: x.update(showgrid=False))
fig.for_each_yaxis(lambda x: x.update(showgrid=False))

fig.show()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

对所有子图设置 showgrid = False 的相关文章

随机推荐