wxPython GUI:将 gnuplot 迁移到 matplotlib
我目前有一个用 wxPython 构建的 GUI,其中有几个部分,其中一个部分显示绘图的 .png 图像:
self.plot = wx.BitmapButton(self.pane_system, -1, wx.Bitmap("/home/myname/projects/newton/plot/src/graph.png", wx.BITMAP_TYPE_ANY))
在 GUI 的另一部分,有一个地方可以编辑参数。当前的设置是,当我更改参数并按 Enter 时,代码告诉 Gnuplot 重新绘制并将新绘图保存到 graph.png 文件中,然后更新 GUI 以显示更新的图像。
我有几个一般性问题:
我想将 gnuplot 迁移到 matplotlib,原因如下:如果我使用 gnuplot,则运行可执行文件的计算机必须在其计算机上安装 gnuplot。另一方面,matplotlib 是一个 python 模块,因此我不必担心在其他机器上安装图形包。这个假设正确吗?
如何修改 wxPython GUI 窗口以显示 matplotlib 图?我发现一些教程告诉我如何使用 matplotlib 图创建新面板,但我只想让它出现在旧图所在的位置。我想我可以让 matplotlib 将绘图保存到图像文件中,就像我在 gnuplot 中所做的那样。将绘图保存为图像并更新 GUI 通常是一个好主意,还是有其他(更快)更新绘图的最佳实践?使用图像文件(如上面的代码)的一个缺点是,当我调整 GUI 窗口的大小时,它们不会调整大小。
如果我将其打包为可执行文件,我是否必须在 Windows 计算机上安装 wxPython/Python 才能使可执行文件运行?
I currently have a GUI built in wxPython with several sections, one of which displays a .png image of a plot:
self.plot = wx.BitmapButton(self.pane_system, -1, wx.Bitmap("/home/myname/projects/newton/plot/src/graph.png", wx.BITMAP_TYPE_ANY))
In another part of the GUI, there is a place where I can edit parameters. The current setup is that When I change the parameters and press enter, the code tells Gnuplot to re-plot and save the new plot to the graph.png file, then update the GUI to show the updated image.
I have several general questions:
I want to migrate gnuplot to matplotlib for the following reason: If I use gnuplot, the machine that is running the executable must have gnuplot installed on their machine. On the other hand, matplotlib is a python module, so I don't have to worry about installing graphical packages on the other machines. Is this assumption correct?
How can I modify my wxPython GUI window to show the matplotlib plot? I found tutorials several telling me how to create a new panel with a matplotlib plot, but I would like to simply have it appear where the old plot was. I think I can make matplotlib save the plot to an image file just as I did in gnuplot. Is it generally a good idea to save the plot as an image and update the GUI, or are there other (faster) best practices for updating plots? One drawback of using image files (as in the above code) is that they do not resize when I resize the GUI window.
If I package this as an executable, will I have to install wxPython/Python on a Windows machine to make the executable run?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
按顺序回答你的问题:
1.)是的,matplotlib 是一个包含的 python 模块。它确实具有外部依赖项,但在 Windows 中,这些依赖项与 matplotlib 安装一起打包。在其他机器上安装时需要担心这些吗?这取决于您要如何安装。你打包成exe了吗?让最终用户安装 Python 和 matplotlib?作为示例,您可以使用 py2exe 将 matplotlib 打包到 exe 中,请参阅此处。当然,您必须为您的后端自定义这些脚本,wx.
2.)您看到带有绘图的面板是因为 matplotlib 提供了 FigureCanvasWxAgg,这是派生自 wxPanel 的 wxWidget,与 matplotlib 配合良好。使用它的优点是您可以为调整大小和绘画等内容设置处理程序。
不过,您的wxBitMapButton正在寻找图像的 wxBitmap。您也许可以为其提供 matplotlib 图的文件句柄 (cStringIO.StringIO),从而无需将文件写入磁盘。您也可以挂钩它的调整大小事件并让 matplotlib 将图形重新绘制到适当的大小。您不会拥有使用FigureCanvasWxAgg 那样的灵活性。不过,我无法对此进行任何研究,因为 wxPython 网站似乎已关闭。
3.) 您可以将wxPython打包成可执行文件。如何取决于您使用的打包程序。我已经用 py2exe 做过很多次了。
Taking your questions in order:
1.) Yes matplotlib is a contained python module. It does have external dependancies but in Windows these dependencies are packaged with the matplotlib install. Do you need to worry about these when you install on other machines? That depends on how you are going to install. Are you packing to an exe? Having the end users install Python and matplotlib? As an example you can package matplotlib into your exe with py2exe, see here. Of course you'll have to customize those scripts for your backend, wx.
2.) You are seeing the panels with plots because matplotlib provides the FigureCanvasWxAgg, which is a wxWidget derived from wxPanel that plays nice with matplotlib. The advantages of using it are that you can set handlers for stuff like resize and painting.
Your wxBitMapButton, though is looking for a wxBitmap for the image. You might be able to give it a file handle (cStringIO.StringIO) to a matplotlib plot and eliminate the need to write a file to disk. You also could probably hook it's resize event and get matplotlib to redraw the figure to the appropriate size. You aren't going to have the amount of flexibility as using the FigureCanvasWxAgg. I can't research any of this, though, as it seems the wxPython web-site is down.
3.) You can package wxPython into executable. How depends on what packager you are using. I've done this with py2exe many times.