먼저 matplotlib을 이용해서 2개의 그래프를 동시에 그려보겠습니다.
plt.figure(figsize=(18,10))
plt.title('Price Prediction & Real Comparison' , fontsize=20)
plt.ylabel('Price Scaled' , fontsize=15)
plt.xlabel('Index' , fontsize=15)
plt.plot(idx_List , Y_test , color = 'blue' , alpha=0.5 , label = 'Real Price')
plt.plot(idx_List , preds , color = 'red' , alpha=0.5 , label = 'Predicted Price')
plt.legend(fontsize=15)
plt.show();
정리하자면 하나의 figure에 2개의 plot을 겹쳐서 그린 것인데 matplotlib에서의 함수 plot의 인자로든 다음이 들어가게 됩니다.
plt.plot(idx_List , Y_test , color = 'blue' , alpha=0.5 , label = 'Real Price')
idx_List는 X축에 들어가게 되고 Y_test는 Y축에 들어가게 되는 것입니다.
하지만 대략 4500개의 x 값들이 있기 때문에 대부분이 겹쳐서 파악이 쉽지 않습니다.
따라서 이 그래프에 확대부분을 그려서 파악해보겠습니다.
먼저 코드를 적기 이전에 결과 그래프는 아래와 같습니다.
과정을 정리하자면 전체 figure을 (2,2) 총 4개로 나누어줍니다.
여기서 (1,1) , (1,2) 는 확대부분을 그리고 아래의 (2,1) , (2,2) 는 합쳐서 전체 그래프를 그리는 것입니다.
fig = plt.figure(figsize=(25,20))
topLeft = fig.add_subplot(2,2,1)
topLeft.set_xlabel('Index' , fontsize = 15)
topLeft.set_ylabel('Price' , fontsize = 15)
sub1_Left = 500
sub1_Right = 600
topLeft.plot(idx_List[sub1_Left:sub1_Right] , Y_test[sub1_Left:sub1_Right] ,
color = 'blue', alpha=0.5 ,
label = 'Real Price')
topLeft.plot(idx_List[sub1_Left:sub1_Right] , preds[sub1_Left:sub1_Right] ,
color = 'red' , alpha=0.5 ,
label = 'Predicted Price')
topLeft.legend(fontsize=10)
먼저 왼쪽 위 그래프는 인덱스를 500 ~ 600으로 좁혀서 그리겠습니다.
topRight = fig.add_subplot(2,2,2)
topRight.set_xlabel('Index' , fontsize = 15)
topRight.set_ylabel('Price' , fontsize = 15)
sub2_Left = 3500
sub2_Right = 3600
topRight.plot(idx_List[sub2_Left:sub2_Right] , Y_test[sub2_Left:sub2_Right] ,
color = 'blue', alpha=0.5 ,
label = 'Real Price')
topRight.plot(idx_List[sub2_Left:sub2_Right] , preds[sub2_Left:sub2_Right] ,
color = 'red' , alpha=0.5 ,
label = 'Predicted Price')
topRight.legend(fontsize=10)
그 다음은 오른쪽 위 그래프의 인덱스는 3500 ~ 3600으로 좁혀서 그리겠습니다.
이제 아래에 전체 그래프를 그려보겠습니다.
bottom = fig.add_subplot(2,2,(3,4))
bottom.set_xlabel('Index' , fontsize = 15)
bottom.set_ylabel('Price' , fontsize = 15)
bottom.plot(idx_List , Y_test , color = 'blue' , alpha=0.5 , label = 'Real Price')
bottom.plot(idx_List , preds , color = 'red' , alpha=0.5 , label = 'Predicted Price')
bottom.legend(fontsize=15)
여기서 각각의 그래프를 그렸으니 각각의 확대부분을 색깔로 칠해보겠습니다.
bottom.fill_between((sub1_Left,sub1_Right), 0, 70, facecolor='green', alpha=0.2)
bottom.fill_between((sub2_Left,sub2_Right), 0, 70, facecolor='orange', alpha=0.2)
이제 여기서 각 그래프를 이어보겠습니다.
con1 = ConnectionPatch(xyA=(sub1_Left, 0), coordsA=topLeft.transData,
xyB=(sub1_Left, 70), coordsB=bottom.transData, color = 'green')
fig.add_artist(con1)
con2 = ConnectionPatch(xyA=(sub1_Right, 0), coordsA=topLeft.transData,
xyB=(sub1_Right, 70), coordsB=bottom.transData, color = 'green')
fig.add_artist(con2)
con3 = ConnectionPatch(xyA=(sub2_Left, 0), coordsA=topRight.transData,
xyB=(sub2_Left, 70), coordsB=bottom.transData, color = 'orange')
fig.add_artist(con3)
con4 = ConnectionPatch(xyA=(sub2_Right, 0), coordsA=topRight.transData,
xyB=(sub2_Right, 70), coordsB=bottom.transData, color = 'orange')
fig.add_artist(con4)
plt.show()
핵심 코드는 아래와 같습니다.
con1 = ConnectionPatch(xyA=(sub1_Left, 0), coordsA=topLeft.transData,
xyB=(sub1_Left, 70), coordsB=bottom.transData, color = 'green')
각 구별된 그래프를 이어주기 위해서 ConnectionPatch를 함수를 이용하면 됩니다.
xyA(sub1_Left , 0) 을 입력함으로써 topLeft의 위치를 입력해줍니다,
그리고 xyB(sub2_Left , 70) 을 입력함으로써 bottom의 위치를 입력해줍니다. 그리고 coords에 각 figure.transData를 그려줍니다.
그리고 이렇게 만든 con1을 아래의 코드로 전체 figure에 추가해줍니다.
fig.add_artist(con1)
결과적으로는 아래와 같은 그래프가 그려집니다.
'Python' 카테고리의 다른 글
Python - Seaborn을 이용해서 데이터 시각화하는 방법 (0) | 2020.11.05 |
---|---|
Python - Matplotlib을 이용해서 데이터 시각화 하는 방법 (0) | 2020.11.05 |
Python - Seaborn 을 이용한 Boxplot , Violinplot 그리기 (0) | 2020.11.01 |
Python - 상관행렬을 이용한 각 속성간의 연관성 파악하기 (0) | 2020.11.01 |
Python - Opencv 를 이용한 얼굴 검출하기 (0) | 2020.11.01 |