【问题标题】:RuntimeError: Can not put single artist in more than one figure when using matplotlib 1.5RuntimeError:使用 matplotlib 1.5 时,不能将单个艺术家放在多个图中
【发布时间】:2016-06-20 18:04:42
【问题描述】:

这是我的代码:

import matplotlib.pyplot as plt
plt.figure(1)                # the first figure
plt.subplot(211)             # the first subplot in the first figure
plt.plot([1, 2, 3])
plt.subplot(212)             # the second subplot in the first figure
plt.plot([4, 5, 6])

plt.figure(2)                # a second figure
plt.plot([4, 5, 6])          # creates a subplot(111) by default
plt.text(.5,1.5,'211',figure = 211) #tring to add text in previous subplot

plt.figure(1)                # figure 1 current; subplot(212) still current
plt.subplot(211)             # make subplot(211) in figure1 current
plt.title('Easy as 1, 2, 3') # subplot 211 title

错误:

Traceback (most recent call last):
File "C:/Users/ezhou/Desktop/python/test3.py", line 11, in <module>
plt.text(.5,1.5,'211',figure = 211)
File "C:\Python27\lib\site-packages\matplotlib\pyplot.py", line 3567, in text
ret = gca().text(x, y, s, fontdict=fontdict, withdash=withdash, **kwargs)
File "C:\Python27\lib\site-packages\matplotlib\axes\_axes.py", line 619, in text
self._add_text(t)
File "C:\Python27\lib\site-packages\matplotlib\axes\_base.py", line 1720, in _add_text
self._set_artist_props(txt)
File "C:\Python27\lib\site-packages\matplotlib\axes\_base.py", line 861, in _set_artist_props
a.set_figure(self.figure)
File "C:\Python27\lib\site-packages\matplotlib\artist.py", line 640, in set_figure
raise RuntimeError("Can not put single artist in "
RuntimeError: Can not put single artist in more than one figure

我试图理解 matplotlib.text.Text() 类中的 kwargs 'figure',但它总是会回复'Can't put single Artist in more than one figure'。所以我对如何使用这个“数字”kwarg 感到困惑。谁能给我一些建议?谢谢!

【问题讨论】:

  • 我不太确定您要对此进行测试的确切内容(在图 1 子图上出现“211”?)但这对我来说 1.4.3 版没有错误。我唯一一次得到任何回溯是plt.show(),但它仍然绘制图表(没有额外的文本)。意思是错误出现在 11 处,但在您的示例中 plt.text(.5,1.5,'211',figure = 211) 是第 10 行。当然它是 1 行,但它只是一个空行吗?
  • 谢谢 roganjosh,我想了解 kwarg 'figure' 的功能。我发现有些人在使用matplotlib 1.5+版本时会报这个错误,而在使用时没有报错
  • 其实我虽然没有报错,但是还是不行。 matplotlib.figure.Figure()plt 不同,因此您不能在 figure= 参数下传递该对象。我现在明白你的问题了,我也很好奇。我认为您在滥用此参数,但实际上我无法弄清楚为什么需要它。快到了……(我想:P)
  • Figure() 用于更新图形,即嵌入在 Tkinter 中的东西。它允许您在嵌入式图形中设置框架的参数,然后更新它显示的数据。您已经接受了无法做到这一点的答案。我应该停止追求这个吗?
  • 谢谢!请继续努力,非常感谢。

标签: python matplotlib


【解决方案1】:

您不应将 figure 作为 kwarg 传递,而应使用 Figure(或 Axes)实例的 text 方法。示例:

import matplotlib.pyplot as plt
fig1, fig2 = plt.figure(1), plt.figure(2)
sp1, sp2 = fig1.add_subplot(211), fig2.add_subplot(211)
sp1.plot([1, 2, 3])
sp2.plot([0, 1, 3])

fig1.text(.5, .3, 'whole figure')
sp2.text(.5, .5, 'subplot')

请注意,坐标是相对的 (0, 1)。

P.S 如果您发现 matplotlib 不必要地复杂(就像我一样),您不妨看看 Plotly

【讨论】:

  • 谢谢!我知道这是可行的方法,但实际上我想知道为什么我不能将数字作为 kwarg 传递?因为“figure”实际上是一个 kwarg。
  • 我不确定你能不能做到。我只知道我无法让它工作。关于该主题的文档非常模糊。我试图查看源代码,但仍然无法弄清楚。
  • 对于未来的读者:如果使用 object-oriented API,matplotlib 会变得更容易和更友好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-19
  • 2012-07-10
  • 1970-01-01
  • 1970-01-01
  • 2014-06-17
  • 1970-01-01
相关资源
最近更新 更多