Plotly 尖峰未出现在子图中

Plotly Spikes Not Appearing on Subplots

我正在制作一个包含多个子图的图形。我希望每个子图都显示尖峰,但无法在第一个子图以外的任何其他地方显示尖峰。我没有看到通过 fig.update_traces 调用设置 showspikes 的能力。有什么建议吗?

重现代码:

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


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

x1 = list(range(100))
y1 = [val**2 for val in x1]

x2 = list(range(150, 250))
y2 = [1./val for val in x2]

fig.add_trace(go.Scatter(x=x1, y=y1), row=1, col=1)
fig.add_trace(go.Scatter(x=x2, y=y2), row=1, col=2)

fig.update_layout(xaxis=dict(showspikes=True))

fig.show()
  • 确保在每个轴上设置 showspikes。你的数字包含 xaxisxaxis2
  • 下面的代码使用 dict 推导来更新所有轴
import plotly.graph_objs as go
from plotly.subplots import make_subplots


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

x1 = list(range(100))
y1 = [val**2 for val in x1]

x2 = list(range(150, 250))
y2 = [1./val for val in x2]

fig.add_trace(go.Scatter(x=x1, y=y1), row=1, col=1)
fig.add_trace(go.Scatter(x=x2, y=y2), row=1, col=2)

fig.update_layout({ax:{"showspikes":True} for ax in fig.to_dict()["layout"] if ax[0:3]=="xax"})

Rob 的回答可能有效,但您可以通过以下方式实现相同的效果:

fig.update_xaxes(showspikes = True)