提问者:小点点

如何为所有子情节设置xticks?


我无法操作所有子图上的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()

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

谢谢你们

R


共2个答案

匿名用户

您可以访问图中的所有xax并使用如下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()

匿名用户

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