居中matplotlib图例(错误条)

uyhoqukh  于 2023-06-30  发布在  其他
关注(0)|答案(1)|浏览(77)

我在试着把标记放在我的图例第二行的中间。
我已经试着按照Centering matplotlib legend entries within incomplete/unfilled rows?的说明,但我不适合我的代码,我不明白为什么。也许是因为我有错误条或者我生成了不同的轴?下面是我的代码示例:

#EXAMPLE OF MY DATA

x = [0, 2, 5, 10, 15]
y1 = [1, 2, 4, 5, 2]
dev1 = [0.2, 0.3, 0.4, 0.6, 0.4]
y2 =[1, 2, 3, 4, 5]
dev2 = [0.01, 0.2, 0.3, 0.4, 0.5]
y3 =[2, 4, 0, 1, 5]
dev3 = [0.1, 0.2, 0.2, 0.4, 0.5]
y4 =[2, 2, 1, 1, 1]
dev4 = [0.1, 0.1, 0.1, 0, 0]
y5 =[2, 2, 1, 2, 2]
dev5 = [0, 0, 0.1, 0, 0]

#####

fig, ax = plt.subplots()
ax.errorbar(x, y1, yerr=dev1, marker="^", markersize=4, linestyle=" ", capsize=4, capthick=2, color="darkorange", label='line1')
ax.errorbar(x, y2, yerr=dev2, marker="s", markersize=4, linestyle=" ", capsize=4, capthick=2, color="green", label='line2')
ax.errorbar(x, y3, yerr=dev3, marker="v", markersize=4, linestyle=" ", capsize=4, capthick=2, color="blue", label='line3')
ax.errorbar(x, y4, yerr=dev4, marker="o", markersize=4, linestyle=" ", capsize=4, capthick=2, color="brown", label='line4')
ax.errorbar(x, y5, yerr=dev5, marker="o", markersize=4, linestyle=" ", capsize=4, capthick=2, color="yellow", label='line5')

handles1, labels1 = ax.get_legend_handles_labels()
handles1 = [h[0] for h in handles1]
nlines=6
ncols=3
handles1.insert(nlines//ncols, plt.plot([],[],color=(0,0,0,0))[0])
labels1.insert(nlines//ncols, " ") #--->I tried to insert this although it raises me an error (there are 6 handles and 5 labels)<----
ax.legend(handles=handles1, labels=labels1, ncol=ncols, framealpha=1)

这是我在最后看到的。

传说并不像预期的那样。我喜欢这样的东西:

tag5nh1u

tag5nh1u1#

我从下面提供的链接中借用了一些信息,沿着this answer。第二个原因是因为您希望水平排序图例条目(按行)而不是按列。那么,让我来过一遍步骤。。
1.一旦你按照你的代码绘制了图表,我使用flip函数将图例转换为水平排序-基本上在第一行标记1到3,在第二行标记4,5。
1.在此之后,我为第一行创建了leg 1,为第二行创建了leg 2。然后,将leg 2的艺术家添加到leg 1,我们就得到了所需的情节。
希望这就是你要找的...

#EXAMPLE OF MY DATA
x = [0, 2, 5, 10, 15]
y1 = [1, 2, 4, 5, 2]
dev1 = [0.2, 0.3, 0.4, 0.6, 0.4]
y2 =[1, 2, 3, 4, 5]
dev2 = [0.01, 0.2, 0.3, 0.4, 0.5]
y3 =[2, 4, 0, 1, 5]
dev3 = [0.1, 0.2, 0.2, 0.4, 0.5]
y4 =[2, 2, 1, 1, 1]
dev4 = [0.1, 0.1, 0.1, 0, 0]
y5 =[2, 2, 1, 2, 2]
dev5 = [0, 0, 0.1, 0, 0]

#####

fig, ax = plt.subplots()
ax.errorbar(x, y1, yerr=dev1, marker="^", markersize=4, linestyle=" ", capsize=4, capthick=2, color="darkorange", label='line1')
ax.errorbar(x, y2, yerr=dev2, marker="s", markersize=4, linestyle=" ", capsize=4, capthick=2, color="green", label='line2')
ax.errorbar(x, y3, yerr=dev3, marker="v", markersize=4, linestyle=" ", capsize=4, capthick=2, color="blue", label='line3')
ax.errorbar(x, y4, yerr=dev4, marker="o", markersize=4, linestyle=" ", capsize=4, capthick=2, color="brown", label='line4')
ax.errorbar(x, y5, yerr=dev5, marker="o", markersize=4, linestyle=" ", capsize=4, capthick=2, color="yellow", label='line5')

## NEW - First flipped the rows and columns...
handles, labels = ax.get_legend_handles_labels()
import itertools

def flip(items, ncol):
    return itertools.chain(*[items[i::ncol] for i in range(ncol)])
ax.legend(flip(handles, 3), flip(labels, 3), ncol=3)

## Get the handles and labels
handles1, labels1 = ax.get_legend_handles_labels()
handles1 = [h[0] for h in handles1]
ncols = 3
nlines = 5  ## This should be the number of entries you have = 5

leg1 = ax.legend(handles=handles1[:nlines//ncols * ncols], labels=labels1[:nlines//ncols * ncols], ncol=ncols)
ax.add_artist(leg1)
leg2 = ax.legend(handles=handles1[nlines//ncols * ncols:], labels=labels1[nlines//ncols * ncols:], ncol=nlines-nlines//ncols*ncols)

leg2.remove()
leg1._legend_box._children.append(leg2._legend_handle_box)
leg1._legend_box.stale = True

plt.show()

相关问题