Python 在PyQt中使用matplotlib图形

Python 在PyQt中使用matplotlib图形,python,matplotlib,pyqt,Python,Matplotlib,Pyqt,在这里编程。我正在尝试在PyQt4 GUI中使用matplotlib小部件。该小部件类似于matplotlib 在某个时候,用户需要单击绘图,我认为类似ginput()的东西可以处理这个绘图。但是,这不起作用,因为图中没有管理器(见下文)。请注意,这与非常类似,但从未得到回答 AttributeError: 'NoneType' object has no attribute 'manager' Figure.show works only for figures managed by pypl

在这里编程。我正在尝试在PyQt4 GUI中使用matplotlib小部件。该小部件类似于matplotlib

在某个时候,用户需要单击绘图,我认为类似ginput()的东西可以处理这个绘图。但是,这不起作用,因为图中没有管理器(见下文)。请注意,这与非常类似,但从未得到回答

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中找到了新的_figure_manager(),但无法使其工作,即使它是正确的解决方案

非常感谢,


James

pyplot
只是面向对象接口的一个包装器,但是它为您再次仔细阅读链接到的示例做了大量工作

FigureCanvas.__init__(self, fig)
线条非常重要,因为它告诉图形要使用什么画布。
图形
对象只是
对象(和一些
文本
对象)的集合,
画布
对象知道如何将
艺术家
对象(即matplotlib的线、文本、点等的内部表示)转换成漂亮的颜色。另请参见另一个嵌入示例,该示例不将FigureCanvas作为子类

有一种方法可以使这个过程变得更容易,但是当我们推出1.4版本时,它就停止了


另请参见:,

是否要处理按钮单击事件?具体来说,是;我使用canvas.connect()来解决这个问题,尽管这无助于我的理解。通常,我希望我的嵌入式matplotlib小部件具有pyplot的方法(gingput等);这是有道理的。我想我是在绕着解决方案转,我只是还没到那里=)我会看看你的作品,谢谢!