2015-05-29 65 views
2

我现在正在使用matplotlib的gridspec模块绘制一个图。我想在特定轴上添加2个不同的图例,如matplotlib: 2 different legends on same graph中提到的图例。在matplotlib的gridspec模块的一个轴上放置2个不同的图例

但是,与gridspec,我总是得到一个传说放在其他轴和一个失去。

这里是我的代码:

import matplotlib.pyplot as plt 
import matplotlib.gridspec as gridspec 

fig = plt.figure() 
gs1 = gridspec.GridSpec(8, 4) 
gs1.update(left=0.1, right=0.65, bottom=0.06, top=1, hspace=0) 

axF = plt.subplot(gs1[0, :]) 
axE = plt.subplot(gs1[1, :],sharex=axF) 
axPA = plt.subplot(gs1[2, :],sharex=axF) 
axMiu = plt.subplot(gs1[3:7, :],sharex=axF) 
axRes = plt.subplot(gs1[7, :],sharex=axF) 

hd1=axMiu.errorbar(range(5),map(lambda x:x+1,range(5)), fmt='o',  color='black', mfc='none',markersize=5, label='hehe') 
hd2=axMiu.errorbar(range(5),map(lambda x:x+2,range(5)), fmt='-',   color='fuchsia',markersize=5, linewidth=2, label='heihei') 

first_legend=plt.legend(prop={'size':12},labelspacing=0,handles=[hd2],loc='upper right') 
axMiu.add_artist(first_legend) 
plt.legend(prop={'size':12},labelspacing=0,handles=[hd1],loc='right') 
plt.show() 

回答

5

你需要使用axis.legend,不plt.legend

例如,作最后的4条线路:

first_legend=axMiu.legend(prop={'size':12},labelspacing=0,handles=[hd2],loc='upper right') 
axMiu.add_artist(first_legend) 
axMiu.legend(prop={'size':12},labelspacing=0,handles=[hd1],loc='right') 
plt.show() 

enter image description here

+0

谢谢,这正是我想要的! –

相关问题