2017-05-18 9 views
1

제목으로 시계열 정렬 작업을하고 있으며 정렬 결과의 시각화가 필요합니다.두 개의 개별 1-D 그림 사이에 점을 연결하는 선 그리기

이렇게하려면 정렬 알고리즘에 의해 생성 된 "앵커 포인트"를 연결하는 선을 그려야합니다. 예에서

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인덱스 개의 시계열 xy, 즉 x[0]y[0]는에 대응된다 간의 일대일 "매핑"을 지정; x[4] 내지 y[9]; 및 x[9] 내지 y[19]. 목표는 두 개의 개별 플롯간에 선을 그어 정렬 결과를 표시하는 것입니다.

+0

'ap'의 두 번째 행의 목적은 무엇입니까? – ImportanceOfBeingErnest

+0

@ImportanceOfBeingErnest 죄송합니다. 매핑 설명에 오타가 있습니다. 나는 그것을 고쳤다. – Francis

답변

1

matplotlib에서 두 개의 서브 그림을 연결하려면 ConnectionPatch을 사용할 수 있습니다. @ImportanceOfBeingErnest에

import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib.patches import ConnectionPatch 

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

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

ls = ["-","--"] 
c = ["gold", "blue"] 

for i, row in enumerate(ap): 
    for j, ind in enumerate(row): 
     px = (ind, x[ind]) 
     py = (ind, y[ind]) 
     con = ConnectionPatch(py,px, coordsA="data", coordsB="data", 
         axesA=ax2, axesB=ax1, linestyle=ls[i], color=c[i]) 
     ax2.add_artist(con) 

plt.show() 

enter image description here

+0

답변 해 주셔서 감사합니다! 나는 OP에서 오타를 고쳤다. 혼란스러워서 미안하다. – Francis

0

덕분에, 나는 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) 

enter image description here

주제에서 벗어나지 만 공백 부분을 자르면 x 축의 범위가 부호에 맞도록 만드는 방법 알 길이, 두 신호 길이의 실제 비율을 유지하면서? sharex=ax1은 신호 길이의 비율을 보여 주지만, 위쪽 그림의 빈 부분은 귀찮습니다.