我试图在图例中显示两个主要的数据集,即“Type2”(红色方块)和“Type1”(蓝色圆圈)。尽管如此,由于我的绘图涉及“类型1”和“类型2”的子组(A、B是每个子组的子组),因此图例中出现了4项。请看一下我的情节:
问题是legend()倾向于显示4项:红方块、红方块、蓝圈、蓝圈,而我只需要其中两项,即红方块表示Type2,蓝圈表示Type1
Type2 Mean2 SD2
A 4.1 1.9
A 5.7 0.9
A 7.5 1.2
B 6.9 0.7
B 4.9 0.4
B 8.5 1
Type1 Mean1 SD1
A 8.1 1
A 7.7 0.9
A 8.5 1.1
B 5.9 0.4
B 7.9 0.7
B 9.5 1.2
Figure1 = plt.figure('Scatter Plot', figsize=(6,6), dpi=300)
Subplot1 = Figure1.add_subplot(1,1,1)
markers = ['s','s']
colors = ['r', 'r']
grouped = DataFrame.groupby('Type2')
for i,((g,d),m,c) in enumerate(zip(grouped,markers,colors)):
x = np.random.normal(loc=i,scale=0.2,size=(len(d['Mean2'],)))
Subplot1.errorbar(x, y= Mean2 , yerr= SD2 ,
fmt=m,
markersize=5, color=c,
capsize=3, markeredgewidth=0.5
)
XPos = list(range(len(grouped)))
Subplot1.set_xticks(XPos)
Subplot1.set_xticklabels([a for a in grouped.groups])
Subplot1.set_xlim(-0.5,1.5)
###############################################
###############################################
markers = ['o','o']
colors = ['b', 'b']
grouped = DataFrame.groupby('Type1')
for i,((g,d),m,c) in enumerate(zip(grouped,markers,colors)):
x = np.random.normal(loc=i,scale=0.2,size=(len(d['Mean1'],)))
Subplot1.errorbar(x, y= Mean1, yerr= SD1,
fmt=m,
markersize=5, color=c,
capsize=3, markeredgewidth=0.5
)
###############################################
###############################################
Subplot1.legend(['Type2','not needed!', 'Type1','not needed!'])
如有任何意见,我们将不胜感激。非常感谢
Matplotlib将只向图例中添加附加了
label
的项。您可以对errorbar
的标签赋值使用三元操作,以便仅为每个groubby对象上的第一次迭代提供一个标签示例:
相关问题 更多 >
编程相关推荐