为所有子图绘制设置 showgrid = False [英] Plotly set showgrid = False for ALL subplots

查看:67
本文介绍了为所有子图绘制设置 showgrid = False的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

完整代码:

import plotly.graph_objects as go从 plotly.subplots 导入 make_subplotsfig = make_subplots(rows=2, cols=2, start_cell=左下")fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6]),行=1,列=1)fig.add_trace(go.Scatter(x=[20, 30, 40], y=[50, 60, 70]),行=1,列=2)fig.add_trace(go.Scatter(x=[300, 400, 500], y=[600, 700, 800]),行=2,列=1)fig.add_trace(go.Scatter(x=[4000, 5000, 6000], y=[7000, 8000, 9000]),行=2,列=2)fig.for_each_xaxis(lambda x: x.update(showgrid=False))fig.for_each_yaxis(lambda x: x.update(showgrid=False))图.show()

I've got 10+ subplots which I want to disable the grid lines for showgrid=False. I need to do that for both x and y axes.

This answer seems to be close, but I can't extend it to do what I want.

In the answer above they show how to set the range for all subplots:

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

But when I try that with showgrid I get an error TypeError: 'NoneType' object does not support item assignment

I tried:

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

Using just update_layout only changes the first subplot:

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

解决方案

Your own suggestion works, but the following is more flexible for an arbitrary number of subplots:

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

Complete code:

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()

这篇关于为所有子图绘制设置 showgrid = False的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆