如何在默认图例旁边添加自定义图例

huangapple go评论79阅读模式
英文:

How to add a custom legend along with default legends

问题

我已经绘制了一些带有定义标签的线条
```Python
plt.axvline(..., label='some text')
plt.legend()

但是,我想要添加一些自定义图例,而不是我们已有的那些。
我知道如何使用from matplotlib.patches import Patch来添加颜色块,或者使用from matplotlib.lines import Line2D来添加线条,代码如下:

legend_elements = [Line2D([0], [0], color='b', lw=4, label='Line'),
                   Line2D([0], [0], marker='o', color='w', label='Scatter',
                          markerfacecolor='g', markersize=15),
                   Patch(facecolor='orange', edgecolor='r',
                         label='Color Patch')]

plt.legend(handles=legend_elements)

但问题是,较新的plt.legend()函数会覆盖默认情况下绘制的旧图例。我希望同时保留默认图例和我自定义定义的图例。

我在寻找能指导我做这个的网站,但大多数只介绍如何添加自定义图例,没有介绍如何与默认图例一起添加。


<details>
<summary>英文:</summary>

I have some lines plotted that has labels defined
```Python
plt.axvline(..., label=&#39;some text&#39;)
plt.legend()

But, I would like to add some custom legends other than the ones we have.
I know how to do it using from matplotlib.patches import Patch for color patches or from matplotlib.lines import Line2D for lines by code like

legend_elements = [Line2D([0], [0], color=&#39;b&#39;, lw=4, label=&#39;Line&#39;),
                   Line2D([0], [0], marker=&#39;o&#39;, color=&#39;w&#39;, label=&#39;Scatter&#39;,
                          markerfacecolor=&#39;g&#39;, markersize=15),
                   Patch(facecolor=&#39;orange&#39;, edgecolor=&#39;r&#39;,
                         label=&#39;Color Patch&#39;)]

plt.legend(handles=legend_elements)

But the problem is that, the newer plt.legend() function overwrites the older legend that was plotted by default. I would like to have both, the default one, along with my custom defined legends.

I was looking for websites that can guide me on this, but I could find mostly on how to add custom legend, but not adding it along with the default legend.

答案1

得分: 2

以下是您要的代码的中文翻译:

这里是一个示例您可以获取旧图例然后将其与您的图例组合

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Patch
from matplotlib.lines import Line2D

t = np.linspace(-10, 10, 100)
sig1 = np.cos(t)
sig2 = np.sin(t)

fig, ax = plt.subplots()
ax.plot(t, sig1, label='Sig 1')
ax.plot(t, sig2, label='Sig 2')
plt.legend()

old_handles, labels = ax.get_legend_handles_labels()

legend_elements = [Line2D([0], [0], color='b', lw=4, label='Line'),
                   Line2D([0], [0], marker='o', color='w', label='Scatter',
                          markerfacecolor='g', markersize=15),
                   Patch(facecolor='orange', edgecolor='r',
                         label='Color Patch')]

plt.legend(handles=old_handles + legend_elements)

希望这有帮助!

英文:

Here is an example, you can get the older legend then combine it with your legend:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Patch
from matplotlib.lines import Line2D

t = np.linspace(-10, 10, 100)
sig1 = np.cos(t)
sig2 = np.sin(t)

fig, ax = plt.subplots()
ax.plot(t, sig1, label=&#39;Sig 1&#39;)
ax.plot(t, sig2, label=&#39;Sig 2&#39;)
plt.legend()

old_handles, labels = ax.get_legend_handles_labels()

legend_elements = [Line2D([0], [0], color=&#39;b&#39;, lw=4, label=&#39;Line&#39;),
                   Line2D([0], [0], marker=&#39;o&#39;, color=&#39;w&#39;, label=&#39;Scatter&#39;,
                          markerfacecolor=&#39;g&#39;, markersize=15),
                   Patch(facecolor=&#39;orange&#39;, edgecolor=&#39;r&#39;,
                         label=&#39;Color Patch&#39;)]


plt.legend(handles=old_handles + legend_elements)

如何在默认图例旁边添加自定义图例

huangapple
  • 本文由 发表于 2023年5月11日 18:23:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76226587.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定