2017-01-29 106 views
1

我有4個不同線的4個子圖,我想在每個子圖的右上角繪製一個圖例,只顯示與該子圖相關的線。我怎麼做?我嘗試了subplot1.legend(),但它會引發錯誤。對於subplot1如何將圖例添加到每個子圖

代碼:

import numpy as np 
from scipy.stats import beta 
from matplotlib import pyplot as plt 

figure = plt.figure("Figure") 
subplot1 = figure.add_subplot(2,2,1) 
x = np.linspace (0, 1.0, 200) 
y1 = beta.pdf(x, 10, 7) 
subplot1.plot(x, y1, "-", label="r'$\alpha=10, \beta=7$'") 
subplot1.legend() #throws an error when I include this statement 
plt.show() 

回答

0

找到了答案!我的標籤中有一個錯字。我改變了標籤:

subplot1.plot(x, y1, "-", label=(r'$\alpha=21, \beta=11$')) 
+1

BTW:你不需要'()''左右r''' – furas

+0

哦,沒事好知道感謝! – 14wml