我正在使用 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
感谢 VeSTLand,关键是使用图形布局的位置属性并缩放 y 轴以正确适应调整。有关基于 VeSTLand 示例代码的多轴的完整实现,请参见下面的 [monstrosity]。
最佳答案
您需要精确组合 make_subplots(rows=1, cols=2)
、add_traces()
和 fig.update_layout(xaxis=dict (域=...)
:
使用
fig=make_subplots(rows=1, cols=2)
设置一个“常规”子图,并包含两条轨迹,如 here 所述.使用
添加第三条轨迹及其自己的 xaxisfig.add_trace(go.Scatter([...[, xaxis="x3"))
然后,使用以下方法调整子图 1 以为
xaxis3
腾出空间:fig.update_layout(xaxis3=dict(anchor="free", overlaying="x1", position= 0.0))
使用
fig.update_layout([...], yaxis2=dict(domain=[0.1, 1]))
进行一些最终调整
之所以必须考虑 domain
是因为 point 3
中的 position
属性不能为负,而且您必须以某种方式为双 x 轴腾出空间。结果如下:
plotly
完整代码:
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
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/