【问题标题】:Plotly: How to set xticks for all subplots?Plotly:如何为所有子图设置 xticks?
【发布时间】:2020-11-22 13:16:03
【问题描述】:

我无法操作所有子图上的 xticks。我正在使用的 xticks 方法(根据文档)仅更改最顶层子图的 xticks。

如何更改下部子图的 xticks?

下面是我的代码:

fig = make_subplots(rows=2, cols=1)

fig.add_trace(
    go.Scatter(x=np.arange(len(df)), y=df['ro2ofst1 ()'], name='ro2ofst1'),
    row=1, col=1
)

fig.add_trace(
    go.Scatter(x=np.arange(len(df)), y=df['ro2ofst2 ()'], name='ro2ofst2'),
    row=2, col=1
)

fig.update_yaxes(range=[0,300],title='mV',row=1,col=1)
fig.update_yaxes(range=[0,300],title='mV',row=2,col=1)
fig.update_xaxes(title_text='data samples',row=2,col=1)

fig.update_layout(title = 'RO2OFST',
                 xaxis = dict(
                 tickmode = 'linear',
                 tick0=0,
                 dtick=25000))
fig.show()

上面的fig.update_layout 仅更改第 1 行第 1 列中子图的 xticks,但底部子图保留默认 xticks。

谢谢

R

【问题讨论】:

  • 我的建议对你有什么效果?

标签: python plotly subplot xticks


【解决方案1】:

我找到了答案。 我可以在 fig.update_layout 中使用 - xaxis1 和 xaxis2

【讨论】:

    【解决方案2】:

    您可以访问 fig 中的所有 xaxis 并使用 for 循环设置范围,如下所示:

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

    剧情:

    完整代码:

    from plotly.subplots import make_subplots
    import plotly.graph_objects as go
    
    fig = make_subplots(rows=3, cols=1)
    
    fig.append_trace(go.Scatter(
        x=[3, 4, 5],
        y=[1000, 1100, 1200],
    ), row=1, col=1)
    
    fig.append_trace(go.Scatter(
        x=[2, 3, 4],
        y=[100, 110, 120],
    ), row=2, col=1)
    
    fig.append_trace(go.Scatter(
        x=[0, 1, 2],
        y=[10, 11, 12]
    ), row=3, col=1)
    
    myRange=[0,6]
    for ax in fig['layout']:
        if ax[:5]=='xaxis':
            fig['layout'][ax]['range']=myRange
    
    fig.update_layout(height=600, width=600, title_text="Stacked Subplots")
    fig.show()
    

    【讨论】:

      猜你喜欢
      • 2020-09-03
      • 2021-10-18
      • 2019-05-13
      • 2021-12-26
      • 2021-07-31
      • 2020-11-25
      • 2018-01-18
      相关资源
      最近更新 更多