手动绘制Matplotlib图例框

时间:2018-10-01 22:10:32

标签: python matplotlib legend

我想合并两个单独的matplotlib图例,以使其看起来像是单个图例(或者更好的是,格式化单个图例,其中单独的行可以具有不同的列数)。到目前为止,我可以按照自己喜欢的方式进行定位(请参见下文),但是我还没有弄清楚如何绘制默认格式的单个灰色圆形框来封装两个图例。有没有简单的方法可以做到这一点?

import matplotlib as mpl
import matplotlib.pyplot as plt

a0 = plt.scatter([1,1],[1,1], color="DarkSeaGreen")
a1 = plt.scatter([1,1],[1,1], color="DarkMagenta")
a2 = plt.scatter([1,1],[1,1], color="salmon")

b0 = plt.scatter([1,1],[1,1], marker='o', facecolors='none', edgecolors='r')
b1 = plt.scatter([1,1],[1,1], marker='o', facecolors='none', edgecolors='b')
b2 = plt.scatter([1,1],[1,1], marker='.', zorder=0, color='wheat')

c0 = mpl.lines.Line2D([], [], color="black")#, label="0")
c1 = mpl.lines.Line2D([], [], color="dimgray")#, label="1")
c2 = mpl.lines.Line2D([], [], color="darkgray")#, label="2")
c3 = mpl.lines.Line2D([], [], color="lightgray")#, label="3")

plt.clf()

mpl.rcParams['figure.figsize'] = (7, 3.75)
mpl.rcParams['figure.dpi'] = 200
mpl.rcParams['font.size'] = 10

fig = plt.figure()

box0 = fig.add_subplot(1, 2, 1)
box1 = fig.add_subplot(1, 2, 2)

fig.subplots_adjust(bottom=0.3, wspace=0.05)
kwargs = dict(borderaxespad=0, bbox_to_anchor=(0.5,0.1))
top = plt.figlegend([a0, b0, a1, b1, a2, b2], ["a0", "b0", "a1", "b1", "a2", "b2"], loc = 'lower center', ncol=3, columnspacing=4.87, **kwargs)
plt.gca().add_artist(top)
plt.figlegend([c0, c1, c2, c3], ["c0", "c1", "c2", "c3"], loc="upper center", ncol=4, **kwargs)

plt.show()

上面生成了以下图像:

output of above code block

而我希望最终图像看起来像这样:

desired output

0 个答案:

没有答案