英文:
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='some text')
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='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)
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='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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论