【问题标题】:use a matplotlib Figure in PyQt在 PyQt 中使用 matplotlib 图
【发布时间】: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


【解决方案1】:

pyplot 只是 OO 接口的包装器,但它为您做了大量工作,您可以仔细阅读您再次链接到的示例,

FigureCanvas.__init__(self, fig)

line 非常重要,因为它告诉图形使用什么画布。 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?

【讨论】:

  • 我了解FigureCanvas;那讲得通。我想我正在寻找解决方案,我只是还没到那里=)我会看看你的工作,谢谢!
猜你喜欢
  • 2015-01-15
  • 2016-01-21
  • 2015-01-05
  • 1970-01-01
  • 1970-01-01
  • 2013-09-08
  • 2011-08-11
  • 2016-08-21
  • 1970-01-01
相关资源
最近更新 更多