带有两行的单个图例项

2024-06-01 05:05:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我想生成一个自定义的matplotlib图例,对于每个条目,每个标签有两行,如本例所示:

enter image description here

根据一些研究,似乎可以简单地提供两个handles' to thefig.legend(句柄、标签)`方法(参见this post作为示例)。但是,如下面的示例代码所示,这只是将这些行相互重叠

import matplotlib.lines as mlines
import matplotlib.pyplot as plt

blue_line = mlines.Line2D([], [], color='r')
green_line = mlines.Line2D([], [], linestyle='--', color='k')
fig, ax = plt.subplots(figsize=(5, 5))
handles = [(blue_line,green_line)]
labels = ['test'] 
fig.legend(handles=handles, labels=labels, fontsize=20)  

因此,我想我要么需要转换一个Line2D对象,要么生成一个包含两行的新Patch对象。然而,我不知道如何做到这一点-有没有一种简单的方法来组合两个补丁,或者我错过了组合手柄的技巧

上下文

如果这对其他人有帮助的话,上下文是我正在使用讨论的技术here,这是一个双轴,它被着色以同时显示两个不同的图。但是,这两行具有相同的标签,因此我想将它们组合在一起


Tags: 方法import示例labelsmatplotlibaslineplt
2条回答

在matplotlib图例指南中有一章介绍custom legend handlers。您可以根据自己的需要进行调整,例如:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.legend_handler import HandlerBase


class AnyObjectHandler(HandlerBase):
    def create_artists(self, legend, orig_handle,
                       x0, y0, width, height, fontsize, trans):
        l1 = plt.Line2D([x0,y0+width], [0.7*height,0.7*height], 
                                                linestyle=' ', color='k')
        l2 = plt.Line2D([x0,y0+width], [0.3*height,0.3*height], color='r')
        return [l1, l2]


x = np.linspace(0, 3)
fig, axL = plt.subplots(figsize=(4,3))
axR = axL.twinx()

axL.plot(x, np.sin(x), color='k', linestyle=' ')
axR.plot(x, 100*np.cos(x), color='r')

axL.set_ylabel('sin(x)', color='k')
axR.set_ylabel('100 cos(x)', color='r')
axR.tick_params('y', colors='r')

plt.legend([object], ['label'],
           handler_map={object: AnyObjectHandler()})

plt.show()

enter image description here

为了有多个这样的条目,可以提供一些参数元组,然后Handler使用这些参数来绘制图例

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.legend_handler import HandlerBase


class AnyObjectHandler(HandlerBase):
    def create_artists(self, legend, orig_handle,
                       x0, y0, width, height, fontsize, trans):
        l1 = plt.Line2D([x0,y0+width], [0.7*height,0.7*height],
                           linestyle=orig_handle[1], color='k')
        l2 = plt.Line2D([x0,y0+width], [0.3*height,0.3*height], 
                           color=orig_handle[0])
        return [l1, l2]


x = np.linspace(0, 3)
fig, axL = plt.subplots(figsize=(4,3))
axR = axL.twinx()

axL.plot(x, np.sin(x), color='k', linestyle=' ')
axR.plot(x, 100*np.cos(x), color='r')

axL.plot(x, .3*np.sin(x), color='k', linestyle=':')
axR.plot(x, 20*np.cos(x), color='limegreen')

axL.set_ylabel('sin(x)', color='k')
axR.set_ylabel('100 cos(x)', color='r')
axR.tick_params('y', colors='r')

plt.legend([("r"," "), ("limegreen",":")], ['label', "label2"],
           handler_map={tuple: AnyObjectHandler()})

plt.show()

enter image description here

这是一个不充分的解决方案,我已经来后,玩了。我张贴它,以防它有助于其他人,但更愿意这样做的正确。它使用两列,第一列的标签作为“/”或者一列可以同样使用“and”

import matplotlib.pyplot as plt

x = np.linspace(0, 3)
fig, axL = plt.subplots()
axR = axL.twinx()

axL.plot(x, np.sin(x), color='k', label='/')
axR.plot(x, 100*np.cos(x), color='r', label='label')

axL.set_ylabel('sin(x)', color='k')
axR.set_ylabel('100 cos(x)', color='r')
axR.tick_params('y', colors='r')

handlesL, labelsL = axL.get_legend_handles_labels()
handlesR, labelsR = axR.get_legend_handles_labels()
handles = handlesL + handlesR
labels = labelsL + labelsR
axR.legend(handles, labels, loc='lower center', ncol=2, fontsize=16,
           handletextpad=0.4, columnspacing=0.4)

enter image description here

相关问题 更多 >