Matplotlib leged使用bbox_to_anchor消失

时间:2014-10-21 21:24:23

标签: python matplotlib legend

我尝试使用bbox_to_anchor命令将matplotlib图例放在自定义位置。我希望左上角位于坐标x = 40和y = 130,所以我的python代码如下所示:

plt.legend([o,h,n,k,l,a], 
           ["data" , "data" , "data" , "data" , "data" , "data"] , 
           scatterpoints = 1 , bbox_to_anchor=(40,130) , loc='upper left' )

当我真正去绘制时,虽然传说已经消失了。如果我删除了bbox_to_anchor选项,则图例会显示在图表上。我的其余代码如下:

import matplotlib.pyplot as plt
from matplotlib import rc

#eu-152 data
eu152_10_1 = ([(10,1)])
#eu152_10_3 = ([(10,3)])
#eu152_10_30 = ([(10,30)])
eu152_10_60 = ([(10,60)])
eu152_10_120 = ([(10,120)])
#eu152_30_1  = ([(30,1)])
eu152_30_3  = ([(30,3)])
eu152_30_30  = ([(30,30)])
eu152_30_60  = ([(30,60)])
eu152_30_120  = ([(30,120)])
eu152_90_1  = ([(90,1)])
eu152_90_3  = ([(90,3)])
eu152_90_30  = ([(90,30)])
eu152_90_60  = ([(90,60)])
eu152_90_120  = ([(90,120)])


plt.rcParams['legend.loc'] = 'best'
plt.figure(1)

#plt.autoscale(enable=True, axis='y', tight=None)
#plt.autoscale(enable=True, axis='x', tight=None)

#extra = Rectangle((0, 0), 1, 1, fc="w", fill=False, edgecolor='none', linewidth=0)
a = plt.scatter(*zip(*eu152_10_1) , color='k', marker='o' , s=300) 
#b = plt.scatter(*zip(*eu152_10_3) , color='r', marker='D' , s=300)
#c = plt.scatter(*zip(*eu152_10_30) , color='r', marker='D' , s=300) 
d = plt.scatter(*zip(*eu152_10_60) , color='m', marker='o' , s=300) 
e = plt.scatter(*zip(*eu152_10_120) , color='r', marker='o' , s=300)
#f = plt.scatter(*zip(*eu152_30_1) , color='r', marker='D' , s=300) 
g = plt.scatter(*zip(*eu152_30_3) , color='b', marker='o', s=300) 
h = plt.scatter(*zip(*eu152_30_30) , color='g', marker='o', s=300) 
i = plt.scatter(*zip(*eu152_30_60) , color='k', marker='o', s=300) 
j = plt.scatter(*zip(*eu152_30_120) , color='m', marker='o', s=300) 
k = plt.scatter(*zip(*eu152_90_1) , color='c', marker='o', s=300) 
l = plt.scatter(*zip(*eu152_90_3) , color='m', marker='o', s=300)
m = plt.scatter(*zip(*eu152_90_30) , color='c', marker='o', s=300) 
n = plt.scatter(*zip(*eu152_90_60) , color='b', marker='o', s=300)
o = plt.scatter(*zip(*eu152_90_120) , color='r', marker='o', s=300)  


rc('text', usetex=True)
plt.ylim(0, 150)
plt.xlim(0, 100)
plt.xlabel('Power Level (kW)', fontsize=26 , labelpad = 20)
plt.ylabel('Irradiation Time (min.)' , fontsize=26)
plt.tick_params(axis='x' , labelsize=20)
plt.tick_params(axis='y' , labelsize=20)
plt.legend([o,h,n,k,l,a], 
               ["data" , "data" , "data" , "data" , "data" , "data"] , 
               scatterpoints = 1 , bbox_to_anchor=(40,130) , loc='upper left' )

plt.xticks([0 , 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 , 100])
plt.yticks([ 1 , 30 , 60 , 90 , 120 ])
plt.grid(True)

print('plots created')
plt.show()

0 个答案:

没有答案