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

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

How to add a custom legend along with default legends

问题

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

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

  1. legend_elements = [Line2D([0], [0], color='b', lw=4, label='Line'),
  2. Line2D([0], [0], marker='o', color='w', label='Scatter',
  3. markerfacecolor='g', markersize=15),
  4. Patch(facecolor='orange', edgecolor='r',
  5. label='Color Patch')]
  6. plt.legend(handles=legend_elements)

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

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

  1. <details>
  2. <summary>英文:</summary>
  3. I have some lines plotted that has labels defined
  4. ```Python
  5. plt.axvline(..., label=&#39;some text&#39;)
  6. 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

  1. legend_elements = [Line2D([0], [0], color=&#39;b&#39;, lw=4, label=&#39;Line&#39;),
  2. Line2D([0], [0], marker=&#39;o&#39;, color=&#39;w&#39;, label=&#39;Scatter&#39;,
  3. markerfacecolor=&#39;g&#39;, markersize=15),
  4. Patch(facecolor=&#39;orange&#39;, edgecolor=&#39;r&#39;,
  5. label=&#39;Color Patch&#39;)]
  6. 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

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

  1. 这里是一个示例您可以获取旧图例然后将其与您的图例组合
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. from matplotlib.patches import Patch
  5. from matplotlib.lines import Line2D
  6. t = np.linspace(-10, 10, 100)
  7. sig1 = np.cos(t)
  8. sig2 = np.sin(t)
  9. fig, ax = plt.subplots()
  10. ax.plot(t, sig1, label='Sig 1')
  11. ax.plot(t, sig2, label='Sig 2')
  12. plt.legend()
  13. old_handles, labels = ax.get_legend_handles_labels()
  14. legend_elements = [Line2D([0], [0], color='b', lw=4, label='Line'),
  15. Line2D([0], [0], marker='o', color='w', label='Scatter',
  16. markerfacecolor='g', markersize=15),
  17. Patch(facecolor='orange', edgecolor='r',
  18. label='Color Patch')]
  19. plt.legend(handles=old_handles + legend_elements)

希望这有帮助!

英文:

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

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from matplotlib.patches import Patch
  4. from matplotlib.lines import Line2D
  5. t = np.linspace(-10, 10, 100)
  6. sig1 = np.cos(t)
  7. sig2 = np.sin(t)
  8. fig, ax = plt.subplots()
  9. ax.plot(t, sig1, label=&#39;Sig 1&#39;)
  10. ax.plot(t, sig2, label=&#39;Sig 2&#39;)
  11. plt.legend()
  12. old_handles, labels = ax.get_legend_handles_labels()
  13. legend_elements = [Line2D([0], [0], color=&#39;b&#39;, lw=4, label=&#39;Line&#39;),
  14. Line2D([0], [0], marker=&#39;o&#39;, color=&#39;w&#39;, label=&#39;Scatter&#39;,
  15. markerfacecolor=&#39;g&#39;, markersize=15),
  16. Patch(facecolor=&#39;orange&#39;, edgecolor=&#39;r&#39;,
  17. label=&#39;Color Patch&#39;)]
  18. 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:

确定