在matplotlib'中的一个轴上放置两个不同的图例;s gridspec模块
我现在正在使用matplotlib的在matplotlib'中的一个轴上放置两个不同的图例;s gridspec模块,matplotlib,plot,Matplotlib,Plot,我现在正在使用matplotlib的gridspec模块进行绘图。我想在一个特定的轴上添加两个不同的图例,如中所述 然而,使用gridspec,我总是会在另一个轴上放置一个图例,而丢失一个图例 这是我的密码: import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec fig = plt.figure() gs1 = gridspec.GridSpec(8, 4) gs1.update(left=0.1, rig
gridspec
模块进行绘图。我想在一个特定的轴上添加两个不同的图例,如中所述
然而,使用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()
您需要使用
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()
谢谢,这正是我想要的!