Python 对于所有子批次,Plotly set showgrid=False

Python 对于所有子批次,Plotly set showgrid=False,python,plotly,Python,Plotly,我有10多个子图,我想禁用showgrid=False的网格线。我需要对x轴和y轴都这样做 似乎很接近,但我不能扩展它来做我想做的事 在上面的答案中,它们显示了如何设置所有子批次的范围: myRange=[0,6] for ax in fig['layout']: if ax[:5]=='xaxis': fig['layout'][ax]['range']=myRange fig.for_each_xaxis(lambda x: x.update(showgrid=Fa

我有10多个子图,我想禁用
showgrid=False
的网格线。我需要对x轴和y轴都这样做

似乎很接近,但我不能扩展它来做我想做的事

在上面的答案中,它们显示了如何设置所有子批次的范围:

myRange=[0,6]
for ax in fig['layout']:
    if ax[:5]=='xaxis':
        fig['layout'][ax]['range']=myRange
fig.for_each_xaxis(lambda x: x.update(showgrid=False))
fig.for_each_yaxis(lambda x: x.update(showgrid=False))
但是当我尝试使用
showgrid
时,我得到一个错误
TypeError:“NoneType”对象不支持项目分配

我试过:

for ax in fig['layout']:
    fig['layout'][ax]['showgrid'] = False
仅使用
update_layout
仅更改第一个子批次:

fig.update_layout(
     xaxis=dict(showgrid=False), 
     yaxis=dict(showgrid=False)
)
我找到了那个。使用
fig['layout']['xaxis7'].更新(showgrid=False)
(1索引)。例如:

范围(10)内的i的
:
图['layout'][f'xaxis{i+1}'].更新(showgrid=False)
图['layout'][f'yaxis{i+1}'].更新(showgrid=False)

您自己的建议可行,但对于任意数量的子批次,以下建议更为灵活:

myRange=[0,6]
for ax in fig['layout']:
    if ax[:5]=='xaxis':
        fig['layout'][ax]['range']=myRange
fig.for_each_xaxis(lambda x: x.update(showgrid=False))
fig.for_each_yaxis(lambda x: x.update(showgrid=False))

完整代码: