繁体   English   中英

在PyQt中使用matplotlib图

[英]use a matplotlib Figure in PyQt

在这里编程菜鸟。 我正在尝试在PyQt4 GUI中使用matplotlib小部件。 该小部件类似于matplotlib的qt示例

在某些时候,用户需要单击该图,我认为类似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中找到了new_figure_manager(),但即使它是正确的解决方案,也无法使它正常工作。

非常感谢,

詹姆士

pyplot只是OO接口的包装器,但是当您仔细阅读链接到的示例时,它做了很多工作,

FigureCanvas.__init__(self, fig)

线条非常重要,因为这就是告诉图形要使用哪种画布的原因。 Figure对象只是Axes对象(和一些Text对象)的集合, canvas对象是知道如何将Artist对象(即matplotlib的线条,文本,点等的内部表示)转换为漂亮颜色的对象。 另请参阅为另一个嵌入示例编写的内容 ,该示例未将FigureCanvas子类FigureCanvas

有一个PR使这个过程更容易,但是当我们得到1.4时它就停滞了。

另请参见: 推荐的哪种绘制方式:matplotlib或pylab? 如何将pyplot函数附加到图形实例?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM