matplotlib中的表图例 [英] Table legend in matplotlib
问题描述
我想在matplotlib中创建一个复杂的图例.我编写了以下代码
I would like to make a complex legend in matplotlib. I made the following code
import matplotlib.pylab as plt
import numpy as np
N = 25
y = np.random.randn(N)
x = np.arange(N)
y2 = np.random.randn(25)
# serie A
p1a, = plt.plot(x, y, "ro", ms=10, mfc="r", mew=2, mec="r")
p1b, = plt.plot(x[:5], y[:5] , "w+", ms=10, mec="w", mew=2)
p1c, = plt.plot(x[5:10], y[5:10], "w*", ms=10, mec="w", mew=2)
# serie B
p2a, = plt.plot(x, y2, "bo", ms=10, mfc="b", mew=2, mec="b")
p2b, = plt.plot(x[15:20], y2[15:20] , "w+", ms=10, mec="w", mew=2)
p2c, = plt.plot(x[10:15], y2[10:15], "w*", ms=10, mec="w", mew=2)
plt.legend([p1a, p2a, (p1a, p1b), (p2a,p2b), (p1a, p1c), (p2a,p2c)],
["No prop", "No prop", "Prop +", "Prop +", "Prop *", "Prop *"], ncol=3, numpoints=1)
plt.show()
它产生类似这样的情节:
It produces plot like that:
但我想在这里绘制复杂的图例:
But I would like to plot complex legend like here:
我也尝试使用table
函数创建图例,但是我无法将补丁对象放入表格中单元格的适当位置.
I also tried to do the legend with table
function but I can not put a patch object into the table to a proper position of a cell.
推荐答案
此解决方案是否足够符合您的喜好?里卡多的答案有点启发,但是我只为每一列使用一个图例对象,然后使用title
-keyword来设置每一列的标题.为了将标记放置在每列的中心,我使用了带有负值的handletextpad
将其向后推.没有个别行的图例.我还必须在标题字符串中插入一些空格,以使它们在屏幕上绘制时看起来同样大.
Is this solution close enough to your liking? It is slighty inspired by Ricardo's answer, but I only used one legend-object for each column, and then utilised the title
-keyword to set the title of each individual column. To put the markers in the center of each column I used handletextpad
with a negative value to push it backward. There are no legends to individual lines. I also had to insert some spaces into the title-strings to make them look equally big when drawn on screen.
我现在还注意到,在保存图形时,还需要对图例框的确切位置进行其他调整,但是由于我想您可能还是想对代码中的其他内容进行调整,因此我将其留给您.您可能还需要玩handletextpad
来使它们完全"对齐.
I also noticed now when the figure was saved that additional tweaks to the exact position of the legend-boxes and are needed, but since I guess you might want to tweak more stuff in the code anyway I leave it for you. You might also need to play yourself with the handletextpad
to make them "perfectly" aligned.
import matplotlib.pylab as plt
import numpy as np
plt.close('all')
N = 25
y = np.random.randn(N)
x = np.arange(N)
y2 = np.random.randn(25)
# serie A
p1a, = plt.plot(x, y, "ro", ms=10, mfc="r", mew=2, mec="r")
p1b, = plt.plot(x[:5], y[:5] , "w+", ms=10, mec="w", mew=2)
p1c, = plt.plot(x[5:10], y[5:10], "w*", ms=10, mec="w", mew=2)
# serie B
p2a, = plt.plot(x, y2, "bo", ms=10, mfc="b", mew=2, mec="b")
p2b, = plt.plot(x[15:20], y2[15:20] , "w+", ms=10, mec="w", mew=2)
p2c, = plt.plot(x[10:15], y2[10:15], "w*", ms=10, mec="w", mew=2)
line_columns = [
p1a, p2a,
(p1a, p1b), (p2a, p2b),
(p1a, p1c), (p2a, p2c)
]
leg1 = plt.legend(line_columns[0:2], ['', ''], ncol=1, numpoints=1,
title='No prop', handletextpad=-0.4,
bbox_to_anchor=[0.738, 1.])
leg2 = plt.legend(line_columns[2:4], ['', ''], ncol=1, numpoints=1,
title=' Prop + ', handletextpad=-0.4,
bbox_to_anchor=[0.87, 1.])
leg3 = plt.legend(line_columns[4:6], ['', ''], ncol=1, numpoints=1,
title=' Prop * ', handletextpad=-0.4,
bbox_to_anchor=[0.99, 1.])
plt.gca().add_artist(leg1)
plt.gca().add_artist(leg2)
plt.gca().add_artist(leg3)
plt.gcf().show()
修改
也许这会更好.您仍然需要调整一些内容,但是bbox的对齐问题已经解决了.
Maybe this will work better. You still have to tweak a few stuff, but the alignment-problem of the bboxes are away.
leg = plt.legend(line_columns, ['']*len(line_columns),
title='No Prop Prop + Prop *',
ncol=3, numpoints=1, handletextpad=-0.5)
这篇关于matplotlib中的表图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!