中文互联网下有很多关于plotly多子图格式的教程,也有关于双Y轴图片的设置,但如果想要在多子图中设置单个子图的双Y轴几乎没有参考文章,其实在plotly官方文档中是有这方面描述的,链接如下。https://plotly.com/python/multiple-axes/https://plotly.com/python/multiple-axes/https://plotly.com/python/multiple-axes/
在官方所提供的例子中,甚至多Y轴也是可以实现的
将官网所列举的例子复制过来,在jupyter中可以直接运行
import plotly.graph_objects as go
from plotly.subplots import make_subplots
fig = make_subplots(rows=2, cols=2,
specs=[[{"secondary_y": True}, {"secondary_y": True}],
[{"secondary_y": True}, {"secondary_y": True}]])
# Top left
fig.add_trace(
go.Scatter(x=[1, 2, 3], y=[2, 52, 62], name="yaxis data"),
row=1, col=1, secondary_y=False)
fig.add_trace(
go.Scatter(x=[1, 2, 3], y=[40, 50, 60], name="yaxis2 data"),
row=1, col=1, secondary_y=True,
)
# Top right
fig.add_trace(
go.Scatter(x=[1, 2, 3], y=[2, 52, 62], name="yaxis3 data"),
row=1, col=2, secondary_y=False,
)
fig.add_trace(
go.Scatter(x=[1, 2, 3], y=[40, 50, 60], name="yaxis4 data"),
row=1, col=2, secondary_y=True,
)
# Bottom left
fig.add_trace(
go.Scatter(x=[1, 2, 3], y=[2, 52, 62], name="yaxis5 data"),
row=2, col=1, secondary_y=False,
)
fig.add_trace(
go.Scatter(x=[1, 2, 3], y=[40, 50, 60], name="yaxis6 data"),
row=2, col=1, secondary_y=True,
)
# Bottom right
fig.add_trace(
go.Scatter(x=[1, 2, 3], y=[2, 52, 62], name="yaxis7 data"),
row=2, col=2, secondary_y=False,
)
fig.add_trace(
go.Scatter(x=[1, 2, 3], y=[40, 50, 60], name="yaxis8 data"),
row=2, col=2, secondary_y=True,
)
fig.show()
代码较为简单,就不讲解了
结果如下,每一个数据点都可以交互