【发布时间】:2014-03-06 23:16:33
【问题描述】:
在这里编程菜鸟。我正在尝试在 PyQt4 GUI 中使用 matplotlib 小部件。该小部件类似于 matplotlib 的 example for qt。
在某些时候,用户需要点击绘图,我认为像 ginput() 这样的东西可以处理。但是,这不起作用,因为该图没有经理(见下文)。请注意,这与another question 非常相似,但从未得到答复。
AttributeError: 'NoneType' object has no attribute 'manager'
Figure.show works only for figures managed by pyplot, normally created by pyplot.figure().
我假设“通常”有办法解决这个问题。
另一个简单的演示脚本:
from __future__ import print_function
from matplotlib.figure import Figure
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 5, 0.1)
y = np.sin(x)
# figure creation by plt (also given a manager, although not explicitly)
plt.figure()
plt.plot(x,y)
coords = plt.ginput() # click on the axes somewhere; this works
print(coords)
# figure creation w/o plt
manualfig = Figure()
manualaxes = manualfig.add_subplot(111)
manualaxes.plot(x,y)
manualfig.show() # will fail because of no manager, yet shown as a method
manualcoords = manualfig.ginput() # comment out above and this fails too
print(manualcoords)
虽然 pyplot 很受欢迎(没有它我几乎找不到答案),但在使用 GUI 时它似乎并不好用。我认为 pyplot 只是 OO 框架的包装器,但我想我只是个菜鸟。
那么我的问题是: 有没有办法将 pyplot 附加到 matplotlib.figure.Figure 的实例? 有没有一种简单的方法可以将经理附加到图形上?我在 matplotlib.backends.backend_qt4agg 中找到了 new_figure_manager(),但无法让它工作,即使它是正确的解决方案。
非常感谢,
詹姆斯
【问题讨论】:
-
你想处理按钮点击事件吗?
-
具体来说,是的;我用 canvas.connect() 来解决这个问题,尽管它对我的理解没有帮助。一般来说,我希望我的嵌入式 matplotlib 小部件具有 pyplot 具有的方法(gingput 等)。
标签: python matplotlib pyqt