[英]use a matplotlib Figure in PyQt
programming noob here. 在这里编程菜鸟。 I'm trying to use a matplotlib widget in a PyQt4 GUI.
我正在尝试在PyQt4 GUI中使用matplotlib小部件。 The widget is similar to matplotlib's example for qt .
该小部件类似于matplotlib的qt示例 。
At some point the user needs to click on the plot, which I thought something like ginput() would handle. 在某些时候,用户需要单击该图,我认为类似ginput()的东西可以处理。 However, this doesn't work because the figure doesn't have a manager (see below).
但是,这不起作用,因为该人物没有经理(请参见下文)。 Note that this is very similar to another question but it never got answered.
请注意,这与另一个问题非常相似,但从未得到解答。
AttributeError: 'NoneType' object has no attribute 'manager'
Figure.show works only for figures managed by pyplot, normally created by pyplot.figure().
I'm assuming by "normally" there's a way around this. 我假设通过“正常”可以解决此问题。
Another simple script to demonstrate: 另一个简单的脚本演示:
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)
As popular as pyplot is (I can't hardly find an answer without it), it doesn't seem to play nice when working with a GUI. 像pyplot一样流行(没有它我几乎找不到答案),在使用GUI时,它似乎不能很好地发挥作用。 I thought pyplot was simply a wrapper for the OO framework but I guess I'm just a noob.
我认为pyplot只是OO框架的包装,但我想我只是菜鸟。
My question then is this: Is there some way to attach pyplot to an instance of matplotlib.figure.Figure? 然后我的问题是:是否可以将pyplot附加到matplotlib.figure.Figure的实例? Is there an easy way to attach a manager to a Figure?
有没有简单的方法可以将经理附加到人物上? I found new_figure_manager() in matplotlib.backends.backend_qt4agg, but couldn't get it to work, even if it is the right solution.
我在matplotlib.backends.backend_qt4agg中找到了new_figure_manager(),但即使它是正确的解决方案,也无法使它正常工作。
Many thanks, 非常感谢,
James 詹姆士
pyplot
is just a wrapper for the OO interface, but it does a lot of work for you read the example you link to again carefully, the pyplot
只是OO接口的包装器,但是当您仔细阅读链接到的示例时,它做了很多工作,
FigureCanvas.__init__(self, fig)
line is very important as that is what tells the figure what canvas to use. 线条非常重要,因为这就是告诉图形要使用哪种画布的原因。 The
Figure
object is just a collection of Axes
objects (and a few Text
objects), the canvas
object is what knows how to turn Artist
objects (ie matplotlib's internal representation of lines, text, points, etc) in to pretty colors. Figure
对象只是Axes
对象(和一些Text
对象)的集合, canvas
对象是知道如何将Artist
对象(即matplotlib的线条,文本,点等的内部表示)转换为漂亮颜色的对象。 Also see something I wrote for another embedding example which does not sub-class FigureCanvas
. 另请参阅我为另一个嵌入示例编写的内容 ,该示例未将
FigureCanvas
子类FigureCanvas
。
There is a PR to make this process easier, but it is stalled while we get 1.4 out the door. 有一个PR使这个过程更容易,但是当我们得到1.4时它就停滞了。
also see: Which is the recommended way to plot: matplotlib or pylab? 另请参见: 推荐的哪种绘制方式:matplotlib或pylab? , How can I attach a pyplot function to a figure instance?
, 如何将pyplot函数附加到图形实例?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.