python - Plotly:如何在同一个子图中显示超过 2 个 x 轴标题/范围?

标签 python plot plotly data-visualization plotly-python

我正在使用 Plotly 并制作具有共享 y 轴和不同 x 轴的散点图子图。我试图使用图形对象 (fig['layout'][data index]) 语法来显示多个堆叠的 x 轴及其各自的范围。通过将“top”和“bottom”分配给图形布局的 side 属性,我只成功地显示了每个子图的两个 xaxes 和范围。下图中右起第二列应显示系列 T5、T6 和 T7 的标题/范围,但只显示 T5 和 T7 的标题和范围。

是否可以在 Plotly 的同一个子图中显示超过 2 个 x 轴标题/范围? For an implemented example, Matplotlib supports showing multiple stacked axes

enter image description here

感谢 VeSTLand,关键是使用图形布局的位置属性并缩放 y 轴以正确适应调整。有关基于 VeSTLand 示例代码的多轴的完整实现,请参见下面的 [monstrosity]。

enter image description here

最佳答案

您需要精确组合 make_subplots(rows=1, cols=2)add_traces()fig.update_layout(xaxis=dict (域=...):

  1. 使用 fig=make_subplots(rows=1, cols=2) 设置一个“常规”子图,并包含两条轨迹,如 here 所述.

  2. 使用 fig.add_trace(go.Scatter([...[, xaxis="x3"))

    添加第三条轨迹及其自己的 xaxis
  3. 然后,使用以下方法调整子图 1 以为 xaxis3 腾出空间:fig.update_layout(xaxis3=dict(anchor="free", overlaying="x1", position= 0.0))

  4. 使用 fig.update_layout([...], yaxis2=dict(domain=[0.1, 1])) 进行一些最终调整

之所以必须考虑 domain 是因为 point 3 中的 position 属性不能为负,而且您必须以某种方式为双 x 轴腾出空间。结果如下:

plotly

enter image description here

完整代码:

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

# initial subplot with two traces
fig = make_subplots(rows=1, cols=2)

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.update_layout(height=600, width=800,
                  title_text="Subplots with shared x-axes")

# extra data where xaxis3 is shared with subplot 1
fig.add_trace(go.Scatter(
    x=[11, 12, 13],
    y=[6, 5, 4],
    name="xaxis3 data",
    xaxis="x3"
))

# some adjustmentns for xaxis3
fig.update_layout(xaxis3=dict(
        title="xaxis3 title",
        titlefont=dict(
            color="#9467bd"
        ),
        tickfont=dict(
            color="#9467bd"
        ),
        anchor="free",
        overlaying="x1",
        side="right",
        position=0.0
    ))

# extra data where xaxis4 is shared with subplot 2
fig.add_trace(go.Scatter(
    x=[50, 60, 70],
    y=[60, 60, 60],
    name="xaxis4 data",
    xaxis="x4",
    yaxis = 'y2'
))

# some adjustments for xaxis4
fig.update_layout(xaxis4=dict(
        title="xaxis4 title",
        titlefont=dict(
            color="#9467bd"
        ),
        tickfont=dict(
            color="#9467bd"
        ),
        anchor="free",
        overlaying="x2",
        side="right",
        position=0.0
    ))

# make room to display double x-axes
fig.update_layout(yaxis1=dict(domain=[0.1, 1]),
                  yaxis2=dict(domain=[0.1, 1]),
                 )

# not critical, but just to put a little air in there
fig.update_layout(xaxis1=dict(domain=[0.0, 0.4]),
                  xaxis2=dict(domain=[0.6, 1]),
                 )

fig.show()

编辑:收紧标题和范围之间的空间。

一种方法是使用 fig.update_layout(title=dict()) 更改标题本身的位置:

fig.update_layout(
    title={
        'text': "Plot Title",
        'y':0.88,
        'x':0.42,
        'xanchor': 'left',
        'yanchor': 'top'})

plotly 2

enter image description here

Plot 2 的完整代码

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

# initial subplot with two traces
fig = make_subplots(rows=1, cols=2)

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.update_layout(height=600, width=800,
                  title_text="Subplots with shared x-axes")

# extra data where xaxis3 is shared with subplot 1
fig.add_trace(go.Scatter(
    x=[11, 12, 13],
    y=[6, 5, 4],
    name="xaxis3 data",
    xaxis="x3"
))

# some adjustmentns for xaxis3
fig.update_layout(xaxis3=dict(
        title="xaxis3 title",
        titlefont=dict(
            color="#9467bd"
        ),
        tickfont=dict(
            color="#9467bd"
        ),
        anchor="free",
        overlaying="x1",
        side="right",
        position=0.0
    ))

# extra data where xaxis4 is shared with subplot 2
fig.add_trace(go.Scatter(
    x=[50, 60, 70],
    y=[60, 60, 60],
    name="xaxis4 data",
    xaxis="x4",
    yaxis = 'y2'
))

# some adjustments for xaxis4
fig.update_layout(xaxis4=dict(
        title="xaxis4 title",
        titlefont=dict(
            color="#9467bd"
        ),
        tickfont=dict(
            color="#9467bd"
        ),
        anchor="free",
        overlaying="x2",
        side="right",
        position=0.0
    ))

# make room to display double x-axes
fig.update_layout(yaxis1=dict(domain=[0.1, 1]),
                  yaxis2=dict(domain=[0.1, 1]),
                 )

# not critical, but just to put a little air in there
fig.update_layout(xaxis1=dict(domain=[0.0, 0.4]),
                  xaxis2=dict(domain=[0.6, 1]),
                 )

fig.update_layout(
    title={
        'text': "Plot Title",
        'y':0.88,
        'x':0.42,
        'xanchor': 'left',
        'yanchor': 'top'})

fig.show()

关于python - Plotly:如何在同一个子图中显示超过 2 个 x 轴标题/范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65056691/

相关文章:

Python 字符串替换 : Keywords into URLs

r - 如何缩短heatmap.2()中ColSideColors的高度

plot - 更改 x 轴以使用 pi 而不是数字进行缩放

python - 禁用向下滚动并以绘图方式显示表格上的所有数据

r - 设置一个绘图缩放以匹配 Shiny 中另一个绘图缩放的缩放

python - 如何在 python 中使用\u00e7 等 unicode 解码文本?

python - 如何将列表转换为数据框矩阵

Python:如何使 'anchor' 文本始终可见和 set_text 方法 Matplotlib

python - 如何阻止 Plotly Scatter 创建不存在的 X 值

python - Twisted 通过任务循环调用从工厂外部 session 向所有客户端发送数据