2017-01-10 4 views
0

중앙 부분 플롯의 위치에 범례를 넣고 싶습니다 (제거하십시오).범례를 서브 플롯의 자리에 놓기

import matplotlib.pylab as plt 
import numpy as np 

f, ax = plt.subplots(3,3) 
x = np.linspace(0, 2. * np.pi, 1000) 
y = np.sin(x) 

for axis in ax.ravel(): 
    axis.plot(x, y) 
    legend = axis.legend(loc='center') 

plt.show() 

내가 중심 플롯을 숨기는 방법을 모르겠어요 이 코드를 썼다. 왜 전설이 나타나지 않습니까?

example plot

이 링크는 코드 몇 가지 문제가 있습니다 http://matplotlib.org/1.3.0/examples/pylab_examples/legend_demo.html

답변

2

도움이되지 않았다. for 루프에서 각 축에 범례를 그려야합니다 (그림이 아닌 축을 loc="center"이라고 함). 그러나 범례에 나타내는 플롯 레이블을 지정하지 않았습니다.

루프에서 중심 축을 선택하고이 축에 대한 범례 만 표시해야합니다. 루프를 반복하고 싶지 않다면 루프의 반복에는 plot 호출이 없어야합니다.

enter image description here

:

import matplotlib.pylab as plt 
import numpy as np 

f, ax = plt.subplots(3,3) 
x = np.linspace(0, 2. * np.pi, 1000) 
y = np.sin(x) 

handles, labels = (0, 0) 

for i, axis in enumerate(ax.ravel()): 

    if i == 4: 
     axis.set_axis_off() 
     legend = axis.legend(handles, labels, loc='center') 
    else: 
     axis.plot(x, y, label="sin(x)") 

    if i == 3: 
     handles, labels = axis.get_legend_handles_labels() 

plt.show() 

이 나에게 다음과 같은 이미지를 제공합니다 : 나는 다음과 같은 코드에서 수행 한 것처럼 당신은 조건문 세트와 함께이 작업을 수행 할 수 있습니다

관련 문제