wxPython GUI: migrating gnuplot to matplotlib - 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?

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.

Related

Using cv2.WINDOW_GUI_EXPANDED to be able to use zoom with imshow()

I need to be able to zoom in and out of an image I show using cv2.imshow().
I read that this can be done by adding a flag that enables an expanded gui display.
cv2.namedWindow('image',cv2.WINDOW_GUI_EXPANDED)
However, running this does not seem to make a difference, and I don't see any buttons on the gui window I create. Is there anything I need to install and how should I run it? I'm not getting any errors.
Thank you.
imshow zooming should work via built-in mouse events without WINDOW_GUI_EXPANDED.
For imshow or your particular use case, there are two requirements for zooming to work:
OpenCV is compiled with Qt library support
A thread is currently free to handle the window events (cv.waitKey(0) blocking in the main thread, or cv.startWindowThread() to spool up a background thread for window events)
I was never able to get QT support to work in windows with python. It works seamlessly in linux. And by building from source on windows with c++, it also works.
I believe that for python you need to install by copying the pyd file in C:\opencv\build\python\cv2\python-3 to your packages folder. Or you need to compile with the global python directories specified in the cmake config.

Is there a way to open graphs in a new window in VSCode?

I'm graphing some stuff with matplotlib and I need to be able to zoom in on the graph. But VSCode just renders it in its Python Interactive window that I can't do anything with. Is there a way to open graphs in a their own window that allows for scaling and things?
I'm a developer on this extension. We don't currently support popping out graphs. If you want to vote up the issue, we have an issue filed for this support here:
https://github.com/Microsoft/vscode-python/issues/4976
Until this support is added there is a bit of a work around. You can manually use the %matplotlib ipython magic to update to non-inline rendering. Then when you show plots they will be shown in a popup window. Like so:
I can't tell you exactly what command to use as it might vary based on what you have installed in your environment. But %matplotlib auto or %matplotlib qt would be good places to try starting with.

Matplotlib figure windows cannot be focused or switched to through cmd+tab

I'm using matplotlib 2.2.2. with ipython 3.6.5 (both from anaconda) for data analysis. Whenever I create a figure window with some boilerplate code like
import matplotlib.pyplot as plt
plt.ion()
plt.plot()
I get a nice figure window to which I can only switch focus by using with the mouse. These figure windows are not part of the list of macosx application switcher (accessible through cmd+tab shortcut). I've tried the osx and the qt5 backends, but I get the same results as described before.
Do you know any solution/workaround for this? It would be very practical to be able to switch focus between figure windows from the keyboard, as I often find myself browsing through the figure windows looking for the graph I'm interested in. Moreover, if matplotlib's figure windows were actual cocoa/macos windows, it would be extremely useful to those out there who use tiling window managers on osx.

Distributing an interactive matplotlib plot

I have a Python script that generates an interactive matplotlib plot with several sliders and radio buttons using the matplotlib.widgets submodule. I'd like to let others play with the resulting plot without having to install python, scipy, numpy, and matplotlib.
I first tried converting my python script to a stand-alone executable that I could distribute. This turned out to be a nightmare - every package I tried (pyinstaller, py2exe, cx_Freeze) failed for one reason or another. The main issue I had was with integrating various scipy and matplotlib libraries, and I'm now very pessimistic about successfully "freezing" my interactive plot.
My next idea was to see if I could get some interactivity through a web browser. Using an IPython notebook with the third-party package JSAnimation initially seemed very promising (http://nbviewer.ipython.org/github/jakevdp/JSAnimation/blob/master/animation_example.ipynb), but it seems that the package only supports the matplotlib "animate" function. This doesn't quite fit the bill for what I'd like to do, as I have multiple sliders which 1) doesn't seem to be supported by this package, and 2) even if supported, would likely result in too many static figures to pre-render effectively, since any such arrangement would require snapshots for every possible combination of the three variables.
Any ideas for how to get this interactive matplotlib plot to others without requiring them to install Python?

Can I use chart modules with wxpython?

Is it possible to use any chart modules with wxpython? And are there any good ones out there?
I'm thinking of the likes of PyCha (http://www.lorenzogil.com/projects/pycha/) or any equivalent. Many modules seem to require PyCairo, but I can't figure out if I can use those with my wxpython app.
My app has a notebook pane, and I'd like to place the chart inside it. The chart has to be dynamic -- ie the user can choose what kind of data to view -- so I'm guessing modules that make chart images are out.
Just for clarity, by charts I mean things like pies, lines and bars etc.
I recently revisited matplotlib, and am pretty happy with the results.
If you're on windows, there are windows installers available to make your installation process a little less painful.
One potential drawback though is that it requires numpy to be installed.
I don't have experience with the interactivity of it, but it does support event handling.
matplotlib does embed quite well in wxpython. I have only used it in Tkinter, which went smoothly for me. I like the optional toolbar that allows direct manipulation of the plot (resizing and panning and such)
Use matplotlib. It integrates nicely with wxPython. Here's a sample of an interactive chart with wxPython and matplotlib.

Categories