【发布时间】: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