Python 在两个单独的一维绘图之间绘制连接点的线

Python 在两个单独的一维绘图之间绘制连接点的线,python,matplotlib,line,sequence-alignment,Python,Matplotlib,Line,Sequence Alignment,作为标题,我正在研究时间序列对齐,需要对齐结果的可视化 为此,我想画一条线来连接对齐算法生成的“定位点” np.random.seed(5) x = np.random.rand(10) # time-series 1 y = np.random.rand(20) # time-series 2 ap = np.array(([0, 4, 9], # the anchor points [0, 9, 19])) fig = plt.figu

作为标题,我正在研究时间序列对齐,需要对齐结果的可视化

为此,我想画一条线来连接对齐算法生成的“定位点”

np.random.seed(5)
x = np.random.rand(10)      # time-series 1
y = np.random.rand(20)      # time-series 2
ap = np.array(([0, 4,  9],  # the anchor points
               [0, 9, 19]))

fig = plt.figure(figsize=(10,5))
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212)
ax1.plot(x, 'r')
ax2.plot(y, 'g')

示例中的锚定点
ap
指定两个时间序列
x
y
指数之间的一对一“映射”,即
x[0]
对应于
y[0]
<代码>x[4]到
y[9]
;和
x[9]
y[19]
。目标是在两个单独的绘图之间绘制线,以显示对齐结果。

要连接matplotlib中的两个子绘图,可以使用


多亏@ImportanceOfBeingErnest,我在OP中识别了打字错误,并实现了两个不同长度系列之间的连接索引:

np.random.seed(5)
x = np.random.rand(10)
y = np.random.rand(20)
ap = np.array(([0, 4, 9],
               [0,9, 19]))

fig = plt.figure(figsize=(10,5))
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212, sharex=ax1)
ax1.plot(x, 'r')
ax2.plot(y, 'g')

plt.setp(ax1.get_xticklabels(), visible=False)

for j in ap.T:

    ax1.axvline(x=j[0], linestyle='--', color='k')
    ax2.axvline(x=j[1], linestyle='--', color='k')

    x_ind = (j[0], ax1.get_ylim()[0])
    y_ind = (j[1], ax2.get_ylim()[1])

    con = ConnectionPatch(y_ind, x_ind, coordsA="data", coordsB="data",
                          axesA=ax2, axesB=ax1, linewidth='1.5')

    ax2.add_artist(con)


我知道这不是主题,但如何进一步截断空白部分以使X轴的范围适合信号长度,同时保持两个信号长度的实际比率?虽然<代码> SEXX= AX1显示了信号长度的比率,但是在顶部图右边的空白部分是恼人的。

第二行的目的是什么?< AP >代码>?@对不起,在映射描述中有错别字。我已经修好了。谢谢你的回答!我修正了操作中的打字错误,抱歉搞混了。
np.random.seed(5)
x = np.random.rand(10)
y = np.random.rand(20)
ap = np.array(([0, 4, 9],
               [0,9, 19]))

fig = plt.figure(figsize=(10,5))
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212, sharex=ax1)
ax1.plot(x, 'r')
ax2.plot(y, 'g')

plt.setp(ax1.get_xticklabels(), visible=False)

for j in ap.T:

    ax1.axvline(x=j[0], linestyle='--', color='k')
    ax2.axvline(x=j[1], linestyle='--', color='k')

    x_ind = (j[0], ax1.get_ylim()[0])
    y_ind = (j[1], ax2.get_ylim()[1])

    con = ConnectionPatch(y_ind, x_ind, coordsA="data", coordsB="data",
                          axesA=ax2, axesB=ax1, linewidth='1.5')

    ax2.add_artist(con)