matplotlib 自定义图例中类别的副标题 [英] Subheadings for categories within matplotlib custom legend

查看:94
本文介绍了matplotlib 自定义图例中类别的副标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的图:

I have a a figure that looks like this:

我想制作一个看起来像这样的图例:

I'd like to make a legend that looks like this:

我该怎么做?

更新:

请注意,此图例有一个带边缘颜色的框架:有效答案将包括此内容.图例也应嵌入轴中.

Note that this legend has a frame with an edgecolor: a valid answer will include this. The legend should also be embedded in the axes.

使用 ax.legend() 可能无法实现我想要的图例.一个很好的答案是展示如何使用补丁和文本或任何有意义的 matplotlib 方法手动构建我想要的图例(如图所示).

The legend I want might not be achievable using ax.legend(). A great answer would be one which shows how to build my desired legend (exactly as shown) manually with patches and texts, or whatever matplotlib methods that make sense.

推荐答案

D 行和 A 行的单独标题:

Separate headings for D and A lines:

from matplotlib.pyplot import *
ds = [1,2,3]
dc = [1.1, 1.9, 3.2]
asim = [1.5, 2.2, 3.1]
ac = [1.6, 2.15, 3.1]

categories = ['simulated', 'calculated']

p1, = plot(ds, 'ko', label='D simulated')
p2, = plot(dc, 'k:', label='D calculated')
p3, = plot(asim, 'b+', label='A simulated')
p4, = plot(ac, 'b-', label='A calculated')
p5, = plot([0], marker='None',
           linestyle='None', label='dummy-tophead')
p7, = plot([0],  marker='None',
           linestyle='None', label='dummy-empty')

leg3 = legend([p5, p1, p2, p5, p3, p4],
              [r'$D_{etc}$'] + categories + [r'$A_{etc}$'] + categories,
              loc=2, ncol=2) # Two columns, vertical group labels

leg4 = legend([p5, p7, p5, p7, p1, p2, p3, p4],
              [r'$D_{etc}$', '', r'$A_{etc}$', ''] + categories + categories,
              loc=4, ncol=2) # Two columns, horizontal group labels

gca().add_artist(leg3)

#If there isn't a big empty spot on the plot, two legends:
#leg1 = legend([p1, p2], categories, title='D_etc', loc=0)
#leg2 = legend([p3, p4], categories, title='A_etc', loc=4)
#gca().add_artist(leg2) 

show()

这篇关于matplotlib 自定义图例中类别的副标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆