Plotly:共享子图子集的 x 轴

问题描述 投票:0回答:3

以下 python 代码将共享所有三个子图的 x 轴:

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

fig = make_subplots(rows=3, cols=1,
                    shared_xaxes=True,
                    vertical_spacing=0.02)

fig.add_trace(go.Scatter(x=[0, 1, 2], y=[10, 11, 12]),
              row=3, col=1)

fig.add_trace(go.Scatter(x=[2, 3, 4], y=[100, 110, 120]),
              row=2, col=1)

fig.add_trace(go.Scatter(x=[3, 4, 5], y=[1000, 1100, 1200]),
              row=1, col=1)

有没有一种简单的方法可以共享前两行的x轴,但允许自由设置第三行的x轴?

我知道可以通过调用各个子图的 update_xaxis 来单独指定范围。想知道是否有另一种方法可以避免这种情况:本着@Ali Taghipour Heidari 建议的精神。

python plotly plotly-python subplot
3个回答
0
投票

可以通过将shared_xaxes设置为[True, True, False]来修复此问题,第一行和第二行的x轴将共享,而第三行的x轴将不与其他行共享。

shared_xaxes=[True, True, False],

0
投票

如果您想要实现的是设置子图中两个图的 x 轴相同,而其余图具有不同的范围,则取消 x 轴共享设置,设置您想要的范围对于两个图来说是通用的,并隐藏 x 轴刻度线。

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

fig = make_subplots(rows=3, cols=1,
                    #shared_xaxes=True,
                    vertical_spacing=0.02)

fig.add_trace(go.Scatter(x=[0, 1, 2], y=[10, 11, 12]),
              row=3, col=1)

fig.add_trace(go.Scatter(x=[2, 3, 4], y=[100, 110, 120]),
              row=2, col=1)

fig.add_trace(go.Scatter(x=[3, 4, 5], y=[1000, 1100, 1200]),
              row=1, col=1)

fig.update_xaxes(range=[0,5], showticklabels=False, row=1, col=1)
fig.update_xaxes(range=[0,5], showticklabels=False, row=2, col=1)

fig.show()


0
投票

使用

{"matches": None}
更新独立轴的布局。以及具有
{"matches": "x"}
的从属轴布局或您希望其匹配的任何轴。 由于某些奇怪的原因,您应该使用“x”而不是“xaxis”,“x2”而不是“xaxis2”等。

# tested with plotly==5.18.0
fig.update_layout(
    {
        "xaxis": {"matches": None},
        "xaxis2": {"matches": "x", "showticklabels": True},
        "xaxis3": {"matches": None},
    }
)
© www.soinside.com 2019 - 2024. All rights reserved.