本文介绍了Plotly:如何在子图中制作无界垂直线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标是在 x=1 的每个子图中获得垂直无限线.在这个例子中,我将在第一行第一列尝试一个 type="line" 的绘图形状

The goal is to get vertical infinite lines in every subplot, at x=1.In this example, I'll just try a single plotly shape of type="line" in the first row, first column

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


fig = make_subplots(
    rows=2,
    cols=2,
    subplot_titles=list(map(str, range(4))),
    shared_xaxes=True,
    shared_yaxes=False,
)

time = np.linspace(-np.pi, np.pi, 1000)

for i in range(4):
    data = np.sin((i+1) * time)
    fig.add_trace(
        go.Scatter(y=data,x=time, name=str(i)),
        row=1 if i in [0, 1] else 2,
        col=1 if i in [0, 2] else 2,
    )

fig.add_shape(
    go.layout.Shape(
        type="line",
        yref="paper",
        xref="x",
        x0=1,
        y0=0,
        x1=1,
        y1=1,
        line=dict(color="RoyalBlue", width=3),
    ),row=1,col=1)

fig.write_image("1.png",width=800, height=600, scale=1)

所以看起来添加具有行和列的形状会覆盖 yref 和外部参照属性,返回一条线段而不是无限线.打印前强制 yref 为纸"...

So it looks like adding a shape with row and column overrides the yref and xref properties, returning a segment of a line instead of an infinite line.Forcing yref to be "paper" before printing...

for shape in fig.layout.shapes:
    shape["yref"]="paper"

...我明白了:

这可以说更糟,一条相对于整个图形而不是子图 y 轴的线.有没有人遇到过这个问题?有什么想法吗?

This is arguably worse, a line that's relative to the whole figure instead of the subplot y axis. Has anyone stumbled with this problem before? Any ideas?

推荐答案

这是你想要的情节吗?

如果是这种情况,那么您必须为由位置 row=i,col=j 定义的每个子图插入一个形状.以下代码段将为您做到这一点.如果您更改子图的总数,您只需要对网格在行数和列数方面的外观进行一些监督.

If that's the case then you'll have to inset a shape to each subplot defined by the positions row=i,col=j. The following snippet will do that for you. If you change the total numbers of subplots You'll just need to have som oversight of how your grid will look with regards to the numbers of rows and columns.

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


fig = make_subplots(
    rows=2,
    cols=2,
    subplot_titles=list(map(str, range(4))),
    shared_xaxes=True,
    shared_yaxes=False,
)

time = np.linspace(-np.pi, np.pi, 1000)

for i in range(4):
    data = np.sin((i+1) * time)
    fig.add_trace(
        go.Scatter(y=data,x=time, name=str(i)),
        row=1 if i in [0, 1] else 2,
        col=1 if i in [0, 2] else 2,
    )

colors = ['blue', 'firebrick', 'green', 'purple']
rows = 2
cols = 2

# add traces
counter=0 # for colors
for i in range(1,3):
    for j in range(1,3):
        fig.add_shape(go.layout.Shape(type="line",
                                        yref="paper",
                                        xref="x",
                                        x0=1,
                                        y0=-2,
                                        x1=1,
                                        y1=2,
                                        #line=dict(color="RoyalBlue", width=3),),
                                        line=dict(color=colors[counter], width=3),),
                      row=i,
                      col=j)
        counter = counter + 1

fig.show()

评论后

据我所知,您不能直接定义无界.但是,只要您定义的轴限制远远超出您想要可视化的数据,您尝试完成的会很好地工作.因为与您的评论相反,您可以像这样设置每个子图的轴限制:

To my knowledge, you can't define an unbounded directly. But what you're trying to accomplish will work pretty good as long as you define axis limits well beyond the data you want to visualize. Because contrary to your comment, you can set the axis limits of each subplot like this:

# Set y ranges for each subplot
for i in range(1,3):
    for j in range(1,3):
        fig.update_yaxes(range=[-4, 4], row=i, col=j)

您可以从数据集中找到合适的最大值和最小值,而不是预定义的限制.

And instead of predefined limits you can find the proper max and min values from your datasets.

图 2: 未缩放

图 3:缩小

我希望这更有帮助!

这篇关于Plotly:如何在子图中制作无界垂直线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 01:02