我试着把记号笔放在我的传奇故事的第二排.
我已经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)
这就是我最后得到的.
传说并不是人们所期待的那样.我想要这样的东西: