Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在matplotlib'中的一个轴上放置两个不同的图例;s gridspec模块_Matplotlib_Plot - Fatal编程技术网

在matplotlib'中的一个轴上放置两个不同的图例;s gridspec模块

在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

我现在正在使用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, 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()

谢谢,这正是我想要的!