当前位置: 首页 > 面试题库 >

Matplotlib图例不起作用

权胜泫
2023-03-14
问题内容

自从升级matplotlib以来,每当尝试创建图例时都会出现以下错误:

/usr/lib/pymodules/python2.7/matplotlib/legend.py:610: UserWarning: Legend does not support [<matplotlib.lines.Line2D object at 0x3a30810>]
Use proxy artist instead.

http://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist

  warnings.warn("Legend does not support %s\nUse proxy artist instead.\n\nhttp://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist\n" % (str(orig_handle),))
/usr/lib/pymodules/python2.7/matplotlib/legend.py:610: UserWarning: Legend does not support [<matplotlib.lines.Line2D object at 0x3a30990>]
Use proxy artist instead.

http://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist

  warnings.warn("Legend does not support %s\nUse proxy artist instead.\n\nhttp://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist\n" % (str(orig_handle),))

这种情况甚至发生在像这样的琐碎脚本中:

import matplotlib.pyplot as plt

a = [1,2,3]
b = [4,5,6]
c = [7,8,9]

plot1 = plt.plot(a,b)
plot2 = plt.plot(a,c)

plt.legend([plot1,plot2],["plot 1", "plot 2"])
plt.show()

我发现错误的链接使我对诊断错误的来源毫无用处。


问题答案:

您应该添加逗号:

plot1, = plt.plot(a,b)
plot2, = plt.plot(a,c)

需要使用逗号的原因是,无论实际上从命令中创建了多少个对象,plt.plot()返回一个行对象的元组。如果没有逗号,则“ plot1”和“
plot2”是元组而不是行对象,从而使以后对plt.legend()的调用失败。

逗号会隐式解压缩结果,以便“ plot1”和“ plot2”自动成为元组中的第一个对象,即您实际想要的线对象,而不是元组。

http://matplotlib.sourceforge.net/users/legend_guide.html#adjusting-the-
order-of-legend-
items



 类似资料:
  • 问题内容: 我正在尝试使形状的动画随着样本大小的增加(从100增加到1000)而呈指数分布。还有一个滑块,用于配置发行版的lambda参数,以及一个用于启动动画的按钮。 我的问题是,即使布局得到渲染,当我按下开始按钮时也没有动画发生。到目前为止,这是我的尝试: 为了简化起见,我在Python笔记本中运行它: 问题答案: 您应该注意函数的名称空间。 考虑 因此,您需要处理实际对象,而不是它们的某些本

  • 问题内容: 我开始使用本教程为初学者学习MatPlotLib 。这是第一个例子。 如果我将这三行代码写入我的python文件并在命令行中执行(通过键入),则什么都不会发生。没有错误信息,没有情节。 有人知道我为什么看不到情节吗? 添加 当然我需要用。但是,即使我添加以下3行: 它仍然不产生任何东西。 添加 这是我现在使用的行: 我仍然有相同的结果(什么都没有)。 问题答案: 后端可能有问题。输出是

  • 如何在IPython笔记本内的Matplotlib中将图例添加到xy线图中?我目前的尝试: 这样做,我得到以下错误: /Users/mc/.virtualenvs/kaggle/lib/python2.7/site-packages/matplotlib/legend.py:613:用户警告:图例不支持[]使用代理艺术家代替。 http://matplotlib.sourceforge.net/u

  • 注:这在1.4中是固定的。3或更高版本 我使用Seaborn plotting软件包,刚刚升级到Matplotlib的最新版本。现在,带有点符号的打印不再渲染。以前功能正常的代码现在创建空白绘图,但只有在导入Seaborn时才创建。下面是一些示例代码: Matplotlib版本: 创建没有seaborn的绘图: 导入seaborn,打印版本: Seaborn版本: 使用海运导入创建线图: 使用海运

  • 这是我的密码。这很简单,但当我点击按钮“button1”时,什么都没有发生。我做错了什么? 第二个-- 第三个-- 布局-- 新Logcat:11-29 12:15:39.552:D/gralloc_goldfish(613):检测到没有GPU仿真的模拟器。11-29 12:15:40.062:I/编舞(613):跳过51帧!应用程序可能在其主线程上做了太多工作。11-29 12:16:08.90

  • 我正在尝试JUnit5TestContainers快速启动示例: