我试着把记号笔放在我的传奇故事的第二排.

我已经try 遵循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)

这就是我最后得到的.

enter image description here

传说并不是人们所期待的那样.我想要这样的东西:

enter image description here

推荐答案

我从你下面提供的链接中borrow 了一些信息,还有this answer个.第二个原因是因为您希望按水平(按行)而不是按列对图例条目进行排序.所以,让我来介绍一下这些步骤.

  1. 根据代码绘制图形后,我使用翻转函数将图例转换为水平排序-基本上在第一行标记1到3,在第二行标记4,5.
  2. 发布之后,我为第一行创建了LEG1,为第二行创建了LEG2.然后,将LEG2的艺术家添加到LEG1中,我们就有了所需的情节.

希望这就是你要找的.

#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()

enter image description here

Python相关问答推荐

Python -根据另一个数据框中的列编辑和替换数据框中的列值

如何使用没有Selenium的Python在百思买着陆页面上处理国家/地区 Select ?

如何在Python中使用io.BytesIO写入现有缓冲区?

Python会扔掉未使用的表情吗?

如何计算两极打印机中 * 所有列 * 的出现次数?

max_of_three使用First_select、second_select、

使用miniconda创建环境的问题

输出中带有南的亚麻神经网络

将数据框架与导入的Excel文件一起使用

Pandas—在数据透视表中占总数的百分比

无法在Docker内部运行Python的Matlab SDK模块,但本地没有问题

如果满足某些条件,则用另一个数据帧列中的值填充空数据帧或数组

matplotlib + python foor loop

为什么常规操作不以其就地对应操作为基础?

Pandas—堆栈多索引头,但不包括第一列

GPT python SDK引入了大量开销/错误超时

根据Pandas中带条件的两个列的值创建新列

我什么时候应该使用帆布和标签?

如何在Python中自动创建数字文件夹和正在进行的文件夹?

如果服务器设置为不侦听创建,则QWebSocket客户端不连接到QWebSocketServer;如果服务器稍后开始侦听,则不连接