python - 使用单图调用时,将图例添加到 pyplot

标签 python matplotlib plot legend

绘制多条线时我习惯了这种语法:

plt.plot(freqs,ps,freqs,psf)

我在其中尝试了 label=["ps","psf"] 的不同变体,使用不同类型的括号,但我总是无法获得正确的图例

最佳答案

plt.plot 将返回线条句柄列表。您可以将它们连同标签列表一起传递给 legend:

handles=plt.plot([0,1],[5,6],[0,1],[8,7])
plt.legend(handles,["label a","label b"])

关于python - 使用单图调用时,将图例添加到 pyplot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35081552/

相关文章:

python - 为什么使用 Seaborn 绘制回归时截距显示不正确?

python - 使用 matplotlib 或 PIL 将 3d numpy 数组绘制为 3d 图像

python - Python 中的位反转

Python - 获取所有目录的文件扩展名

python - 使用 Python 请求将数据传递到 Django View

python - 在 fedora 19 上从源代码构建 matplotlib,没有发现 freetype 头文件

python-3.x - 具有嵌套行的 Seaborn 热图

python - 如何更改绘图线样式并使用两个 Y 轴代替一个 X 轴?

Python:将表达式解释为文本而不计算它

plot - gnuplot:带有误差条的行堆积条形图