2017-12-14 1 views
0

그림 상자 중 하나의 플롯이 표시되지 않습니다. 두 개의 그림은 하나의 그림 상자 안에 있지만 각각 하나의 그림 안에는 그려져 있지 않습니다. 아래는 제가 의미하는 바를 묘사 한 것입니다 : enter image description here서브 플로트의 그림이 보이지 않음

각 그림 상자는 그림 상자로 구분되어야합니다. 여기에 플로팅에 대한 코드는 다음과 같습니다

ax = fig.add_subplot(2,1,1) 
cm = plt.get_cmap('gist_rainbow') 
ax.set_color_cycle([cm(1.*i/NUM_COLORS_RIGHT) for i in range(NUM_COLORS_RIGHT)]) 
ax.grid(True) 
plt.title('Right IRI data per mile for verification runs:') 
plt.tick_params(axis='both', which='major', labelsize=8) 
plt.hold(True) 

ax = fig.add_subplot(2,1,2) 
cm = plt.get_cmap('gist_rainbow') 
ax.set_color_cycle([cm(1.*i/NUM_COLORS_LEFT) for i in range(NUM_COLORS_LEFT)]) 
ax.grid(True) 
plt.title('Left IRI data per mile for verification runs:') 
plt.tick_params(axis='both', which='major', labelsize=8) 
plt.hold(True) 

for i in range(NUM_COLORS_RIGHT): 
    plt.subplot(2,1,1) 
    ax.plot(dataR[i],linewidth=2) 
    if dataR[i] != dataR[-1]: 
     continue 
    else: break 

for d in range(NUM_COLORS_LEFT): 
    plt.subplot(2,1,2) 
    ax.plot(dataL[d],label = d,linewidth=2) 
    ax.legend(loc='lower right',borderaxespad=-4,bbox_to_anchor=(1.062, 0.2),ncol=1) 
    if dataL[d] != dataL[-1]: 
     continue 
    else: break 

plt.show() 
plt.close('all') 

그래서 코드는 다른 기능과 장소에서 데이터를 가져와 그 좌우 데이터 모두에 대한리스트 목록에 데이터. 변수 NUM_COLORS_RIGHT 및 NUM_COLORS_LEFT는 각면에 대한 목록 내에있는 목록의 수를 나타냅니다. 그런 다음 이러한 변수를 사용하여 데이터를 그리는 데 사용해야하는 색상 수를 결정하여 색상 맵을 사용하고 불특정 다수의 텍스트 파일을 플로팅 할 때 색상 재사용을 방지합니다.

두 개의 그림이 모두 아래에 있으므로 dataR [I]의 그림을 위의 그림으로 이동해야합니다.

답변

1

두 개의 matplotlib 코딩 스타일 - 객체 지향 인터페이스와 상태 시스템이 섞여 있습니다. 두 서브 루틴 모두에 변수 이름 ax을 사용하므로 첫 번째 변수에 대한 참조가 손실되므로 ax.plot을 사용하여 그 변수에 플롯 할 수 없습니다. ,

ax1 = fig.add_subplot(2,1,1) 
cm = plt.get_cmap('gist_rainbow') 
ax1.set_color_cycle([cm(1.*i/NUM_COLORS_RIGHT) for i in range(NUM_COLORS_RIGHT)]) 
ax1.grid(True) 
ax1.set_title('Right IRI data per mile for verification runs:') 
ax1.tick_params(axis='both', which='major', labelsize=8) 
plt.hold(True) 

ax2 = fig.add_subplot(2,1,2) 
cm = plt.get_cmap('gist_rainbow') 
ax2.set_color_cycle([cm(1.*i/NUM_COLORS_LEFT) for i in range(NUM_COLORS_LEFT)]) 
ax2.grid(True) 
ax2.set_title('Left IRI data per mile for verification runs:') 
ax2.tick_params(axis='both', which='major', labelsize=8) 
plt.hold(True) 

for i in range(NUM_COLORS_RIGHT): 
    ax1.plot(dataR[i],linewidth=2) 
    if dataR[i] != dataR[-1]: 
     continue 
    else: break 

for d in range(NUM_COLORS_LEFT): 
    ax2.plot(dataL[d],label = d,linewidth=2) 
    ax2.legend(loc='lower right',borderaxespad=-4,bbox_to_anchor=(1.062, 0.2),ncol=1) 
    if dataL[d] != dataL[-1]: 
     continue 
    else: break 

plt.show() 
plt.close('all') 

둘 다 줄거리 이름을 원하지 않는 경우

, 당신은 다시 plt.subplot는 관련 부가 적 줄거리 활성화 사용할 수 있습니다

당신은 서로 다른 두 축 뭔가를 부를 수있는, 그 주위를 얻으려면 두 번째 예제의 동작은하기 matplotlib의 이후 버전에서는 더 이상 사용되지 않습니다 것을

ax = fig.add_subplot(2,1,1) 
cm = plt.get_cmap('gist_rainbow') 
ax.set_color_cycle([cm(1.*i/NUM_COLORS_RIGHT) for i in range(NUM_COLORS_RIGHT)]) 
ax.grid(True) 
plt.title('Right IRI data per mile for verification runs:') 
plt.tick_params(axis='both', which='major', labelsize=8) 
plt.hold(True) 

ax = fig.add_subplot(2,1,2) 
cm = plt.get_cmap('gist_rainbow') 
ax.set_color_cycle([cm(1.*i/NUM_COLORS_LEFT) for i in range(NUM_COLORS_LEFT)]) 
ax.grid(True) 
plt.title('Left IRI data per mile for verification runs:') 
plt.tick_params(axis='both', which='major', labelsize=8) 
plt.hold(True) 

for i in range(NUM_COLORS_RIGHT): 
    plt.subplot(2,1,1) 
    plt.plot(dataR[i],linewidth=2) 
    if dataR[i] != dataR[-1]: 
     continue 
    else: break 

for d in range(NUM_COLORS_LEFT): 
    plt.subplot(2,1,2) 
    plt.plot(dataL[d],label = d,linewidth=2) 
    plt.legend(loc='lower right',borderaxespad=-4,bbox_to_anchor=(1.062, 0.2),ncol=1) 
    if dataL[d] != dataL[-1]: 
     continue 
    else: break 

plt.show() 
plt.close('all') 

주, 그래서 첫 번째 방법은 아마도 가장 안전 :하지만 당신은 ax.plot 대신 plt.plot를 사용해야합니다.

MatplotlibDeprecation 경고 : 이전 축과 동일한 인수를 사용하여 축을 추가하면 현재 이전 인스턴스가 다시 사용됩니다. 이후 버전에서는 항상 새 인스턴스가 생성되어 반환됩니다. 한편, 각 축 인스턴스에 고유 한 레이블을 전달하여이 경고를 억제하고 향후 동작을 보장 할 수 있습니다.

+0

와우는 많은 의미가 있습니다. 아픈 지금 당장 시도하십시오 –

+0

나는 첫 번째 코드가 가장 대답을 좋아한다 –

+0

그것은 정말 고마워 했어! –

관련 문제