这里是编程菜鸟。我正在尝试在 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() ,但无法让它工作,即使它是正确的解决方案。
非常感谢,
詹姆斯
最佳答案
pyplot
只是 OO 接口(interface)的包装器,但它做了很多工作,让您仔细阅读链接到的示例,
FigureCanvas.__init__(self, fig)
线条非常重要,因为它告诉图形要使用什么 Canvas 。 Figure
对象只是 Axes
对象(和一些 Text
对象)的集合,canvas
对象是知道如何将Artist
对象(即matplotlib的线条、文本、点等的内部表示)转换为漂亮的颜色。另请参阅something I wrote另一个嵌入示例,它没有子类 FigureCanvas
。
有一个PR使这个过程变得更容易,但当我们推出 1.4 时,它就停滞了。
另请参阅:Which is the recommended way to plot: matplotlib or pylab? , How can I attach a pyplot function to a figure instance?
关于python - 在 PyQt 中使用 matplotlib 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22217215/