在 PyQt5 中嵌入 matplotlib 的示例 [英] example of embedding matplotlib in PyQt5

查看:320
本文介绍了在 PyQt5 中嵌入 matplotlib 的示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能展示一个简单的例子,其中包含在 PyQt5 中嵌入 matplotlib 的基本功能?

Can anybody show a simple example with basic functionality of embedding matplotlib in PyQt5?

官方 docs 中提供了PyQt4示例.PyQt4 很快就会过时:

There is PyQt4 example available in official docs. PyQt4 will soon be outdated:

Digia宣布将于2015年底停止对Qt v4的支持.强烈建议在所有新开发中使用PyQt5和Qt v5.

Digia have announced that support for Qt v4 will cease at the end of 2015. PyQt5 and Qt v5 are strongly recommended for all new development.

可能是任何一般示例,例如Qt5 QMainWindow/QWidget + matplotlib线图.

It could be any general example, say, Qt5 QMainWindow/QWidget + matplotlib line plot.

推荐答案

您可能正在寻找的内容,可以在这里找到:

what you might be looking for, can be found here: embedded matplotlib in PyQt. It's a blog of one of matplotlib's developers. It is stil written for PyQt4 but it turned out that one just has to change all PyQt4's to 5 and replace QtGui with QtWidtgets. I talked to Ryan and send him the updated code. He was pleased to see that there are people who aknowledge his effort and will updated his tutorial.

由于我遇到了同样的问题,并且一直在寻找一些解决方案以开始使用,因此我发现 zedcode PyQt5介绍性教程,不涉及matplotlib,但可以组合起来帮助理解.

Since I had the same problem and was looking around for some solutions to get a start, I found zedcode PyQt5 Introductory Tutorial which doesn't cover matplotlib but can be combined and helps to understand.

最后,让我说一些关于将 matplotlib 绘图转换为 Gui 必须做的事情.Ryan 的 tut 指出,必须构建一个普通的 QWidget 来保存 canvas.您可以使用 Qt Creator 做到这一点.gui和matplotlib连接的部分如下:

Finally, let me say something aobut what one has to do to get matplotlib plots into a Gui. Ryan's tut points out, that one has to build a normal QWidget which will hold the canvas. You can do that with Qt Creator. The part where gui and matplotlib join is the following:

def addmpl(self, fig):
    self.canvas = FigureCanvas(fig)
    self.mplvl.addWidget(self.canvas)
    self.canvas.draw()

mplvl只是一个空的 QWidget ,它获取 FigureCanvas ,并在随后显示.对于我自己的应用程序,我想显示 3D 绘图,但是在删除鼠标支持方面存在一些问题,因此可能会留下一些问题.结果证明所有 2D 绘图功能齐全.

mplvl is just the empty QWidget which gets the FigureCanvas and is shown afterwards. For my own app, I wanted to show a 3D-plot, but there are some issues with dropping the mouse support, so there could be some problems left. All 2D plots turned out to be fully functional.

试试看.

基督徒

这篇关于在 PyQt5 中嵌入 matplotlib 的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆