PyPlot show() really slow after calling PyQt4.QFileDialog.getOpenFileNames() - matplotlib

For an analysis application I'm trying to:
Get a list of filenames from the user using PyQt4 QFileDialog
Extract some data from each file
Plot the aggregated data using pyplot
However, getting the filenames this way causes pyplot.show() to take a really, really long time. I've distilled the problem down to the following test case:
import matplotlib.pyplot as plt
from PyQt4 import QtCore, QtGui
app = QtGui.QApplication(None)
fd = QtGui.QFileDialog(None)
filenames = fd.getOpenFileNames(caption='Select Data Files')
plt.plot([1,2,3,4,5])
plt.show()
Note that I'm not even doing anything with the filenames here--just getting them.
Selecting a single file from the dialog results in a plot time of 10 seconds. Interestingly, the time required for show() to complete goes up roughly linearly with the number of files selected. For 10 files it takes about 67 seconds for the plot to show.
This isn't a big deal for plotting data from a handful of files, but when aggregating data from Monte Carlo simulations where there are thousands of files it can take literally hours for the plot to show. Telling matplotlib to use the Qt4Agg backend results in the same behavior.
If I comment out the call to getOpenFileNames the script will complete in under a second.
I'm running the following versions of the relevant packages (should be latest):
Python 2.7
matplotlib 1.3.1
python-sip 4.13.2-1
python-qt4 4.9.1-2ubuntu1
python-sip-dev 4.13.2-1
python-qt4-dev 4.9.1-2ubuntu1
I uninstalled sip, qt4 and reinstalled them--same issue. I've seen the issue accross two separate machines--both running 32-bit Ubuntu 12.04.
Any help would be much appreciated--I've wasted an embarrassing amount of time waiting for plots to show.
Updates:
The type and name of the files selected doesn't seem to matter.
Cancelling or exiting the file dialog box results in no delay and an immediate plot.
Running the script as sudo eliminates the issue; however, the file dialog looks different when I run as sudo and may be using a different gui backend that doesn't conflict with pyplot's use of PyQt, so may be a red herring.
The program doesn't hang in getOpenFileNames, it hangs on the next call that uses PyQt. Whether that's a plot or another file dialog doesn't seem to matter--the first file dialog blocks both.
Calling app.processEvents() after running the dialog doesn't help.
Using PySide in place of PyQt4 results in the same behavior.
Using getOpenFileName instead of getOpenFileNames results in the same behavior.
Running getOpenFileNames with the DontUseNativeDialog option works (no delay)
None of the other QFileDialog options have any effect (ShowDirsOnly, DontResolveSymlinks, DontConfirmOverwrite, ReadOnly, HideNameFilterDetails, DontUseSheet)

Related

IPython console: Display plots when using run

I'm using a IPython console (IPython 8.3.0) for running scripts and then use the interactive console to manually inspect variables, try something new, etc.
My problem is with plotting figures - I'm looking for a way to display figures when using e.g. %matplotlib auto and %run myscript.py in the IPython console. I expect the figures drawn in separate windows, not inline.
I'm able to get the behavior I want by using %load myscript.py and executing the code, but this is quickly cluttering the history in the console.
I want to avoid using matplotlib.pyplot.show() in my scripts - if possible.
I there a way to achieve what I want without using %load or show()?

DataFrame (pandas) plot opens window but there is no plot

I am unable to produce any charts in Python (matplotlib 3.5.1) with the pandas DataFrame plot() method. A window opens and the axes return value is <AxesSubplot:> as opposed to returning an object like that prints as somethig like <matplotlib.axes._subplots.AxesSubplot at 0x7f3958bcf9d0>, which is what I usually see when the plot works.
The backend is QtAgg and as far as I can tell from a poke between fora pages that report a similar problem this should be all right. This is also not a problem with needing to run matplotlib.pyplot.ion() as I see a window open, but it is black with no plot in it.
Any advice would be useful, thanks!
I have resolved the (mis)behaviour without really being sure that I have resolved the issue responsible for this misbehaviour.
As looked around for a way out, I found this discussion (How to change backends in matplotlib / Python) which made a lot of getting the back end setting right. Wondering if that would make a difference, I changed the back end. The QtAgg back end loaded by default from the matplotlibrc file and so following the instructions in the post on how to permanently change the back end, I modified the file so that the back end is now Qt5Agg. The data now plots.

matplotlib automatically shows figure

I am using matplotlib version 3.0.3 in Eclipse Oxygen 3 on Windows 10.
import matplotlib.pyplot as plt
fig, axes = plt.subplots(nrows=4, ncols=4) # Figure is shown!?
plt.show() # expected to see figure only here
It seems that whenever a plt method gets called matplotlib already opens the UI.
That is useful while debugging but I usually want to just savefig and not stop to look at the generated image ...
This used to work fine for me on my previous Window 7 laptop (not sure what matplotlib version).
Has something changed in the default behavior which I cannot find documented?
Stepping into the matplotlib code I've discovered that interactive mode was set -- probably by default as I don't have a matplotlibrc file anywhere.
I added matplotlib.interactive(False) at the beginning of main() to solve this.
Apparently I still get the interactive mode when debugging even with the above line, so this is good enough for me (for now).

matplotlib doesn't show figures after converting to exe file

Everything looks OK if I just run the scripts in Pycharm. It will show the plots.
However, after I convert the scripts into exe file. It can save the plots, but it won't show any plot. The most weird thing is that it will re-open another exe file.
Thanks, really appreciate if someone know the root cause.
As mentioned in the comment, calling matplotlib.use('WXAgg') is necessary, but there is another step: matplotlib has a backend object you need to import manually to paint a figure in wx: matplotlib.backends.backend_wxagg. This creates a figure canvas object inside a wx.panel of your choosing.
This answer contains a nice example. Furthermore, you can look up the actual object in matplotlib website, then proceed to look at their artist tutorial, which covers how to use matplotlib in applications rather well.

PTVS plotting pandas Dataframes and Series with matplotlib in python debug interactive

Is it possible to plot pandas objects inside the PTVS interactive debugger? Is it possible to save plots to disk as jpeg's?
I think I was able to do this when I first started using PTVS (last year, its awesome by the way!) but I just tried again and I dont get any plots appearing. I cant remember if I had to do something special to get this to work and from doing some google searches I get a confusing picture of the current best practice in this regard.
I want to be able to plot diagrams from my debug interactive window, similar to what is shown on this pandas tutorial.
http://pandas.pydata.org/pandas-docs/stable/visualization.html
Is this possible?
Visual Studio Professional 2013 update 4 (latest I think)
PTVS 2.1.21008.00 (latest I think)
All help is greatly appreciated.
-Jason
[edit: more info on this http://pytools.codeplex.com/discussions/574776 ]
Unfortunately not. The regular interactive has an IPython mode that, among other things, enables inline plots. But the Debug interactive doesn't have that.
You can, of course, still load matplotlib in the Debug interactive and tell it to plot things. But because there's no integration of event loops between it and VS in that mode, the plots will basically work like modal windows - you won't be able to continue debugging or otherwise interact with VS until you close the plot window.