Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MatPlotLib Figure Freezing on Windows #2134

Closed
dmikkili opened this issue Jun 18, 2013 · 20 comments
Closed

MatPlotLib Figure Freezing on Windows #2134

dmikkili opened this issue Jun 18, 2013 · 20 comments

Comments

@dmikkili
Copy link

Python version: 2.7; Windows version: 7; Matplotlib version 1.2.1

I'm making an interactive plot (set with plt.ion()) while some other parts of my script do some calculations, and I update the plot frequently with calls to plt.draw(). I noticed that when I click the figure window of the interactively drawn plot and try to move it around, the figure stops updating and Windows tags the process "Not Responding", but the script continues to run without a problem. If I don't click the figure window, everything is ok.

I've read that some people have had this issue before on other forums and they got around the problem by using different backends, but my computer doesn't respond differently to other backends. What can I do?

@bmorris3
Copy link

HI all, I'm debugging with @dmikkili . I've found that a short example script that duplicates this issue on his machine is this:

from matplotlib import pyplot as plt
import time

plt.ion()
fig = plt.figure()
axis = fig.add_subplot(111)

for i in range(30000):
    axis.plot(i,i,'o')
    plt.draw()
    if i > 5:
        time.sleep(2)   ## On Windows, a pause is needed to be able to click the figure at all
plt.close()

This replicates the same mpl calls that we make in the much longer script where the issue occurs. I do not have any problems moving around the figure produced by this script as it changes on Mac OS X 10.6, Python 2.7, mpl 1.2.1.

@dmcdougall
Copy link
Member

@cgohlke Can you reproduce this?

@cgohlke
Copy link
Contributor

cgohlke commented Jun 23, 2013

I can reproduce this with matplotlib-1.3.0rc4.

@mdboom
Copy link
Member

mdboom commented Jun 24, 2013

Which backend are you using, and does trying an alternative make any difference?

@bmorris3
Copy link

My default backend is "MacOSX" (and I do not get the error).

@mdboom
Copy link
Member

mdboom commented Jun 24, 2013

Some data points. On Linux, TkAgg works, GtkAgg stops responding (but continues to update), Qt4Agg never displays at all, and WxAgg (being Gtk under the hood) stops responding (but continues to update).

@mdboom
Copy link
Member

mdboom commented Jun 24, 2013

Do we know which backend on Windows is failing, though?

@cgohlke
Copy link
Contributor

cgohlke commented Jun 24, 2013

On Windows, all GUI backends are failing: TkAgg, WxAgg, GTKAgg, Qt4Agg. Qt4Agg does not display anything, the others are unresponsive.

@bmorris3
Copy link

Hi all, I'm still having this issue. Are you guys? @dmikkili

@dmikkili
Copy link
Author

Yes I am still having this error. I am using WxAgg by the way. Is there a solution/workaround for this issue?

@efiring
Copy link
Member

efiring commented Aug 23, 2013

This is not a mode of operation that we have ever supported; on some backends, on some platforms (Mike noted TkAgg on Linux) one can get away with it, but this is an accident, not something designed into mpl. Interactive mode is designed to work with ipython. That's all. The longstanding decision to delegate this to ipython could be revisited, but I suspect that no matter what we do, there will be problems with some environments.

The documentation, http://matplotlib.org/users/shell.html, might need some more work to clarify the situation. Part of the problem might be the distinction between working interactively from a shell, and having a script that updates a plot based on calculations.

Here is a modification of the example script above that I think will work with any interactive backend. Note that "ion()" is not needed--it is irrelevant here.

import matplotlib
matplotlib.use("qt4agg")
from matplotlib import pyplot as plt

fig = plt.figure()
axis = fig.add_subplot(111)

for i in range(30000):
    axis.plot(i,i,'o')
    if i > 5:
        plt.pause(1)
plt.close()

This probably is not what you really want, though; I suspect you want long-running calculations combined with nicely-behaved interactive plots. The problem is that the window will not be responding to events (e.g., resize) during those calculations, unless you do them in a separate process, or a separate thread, or unless you sprinkle plt.pause(0.001) liberally throughout your calculation code to force gui event processing.

Generally, the pyplot interface is not suitable for this sort of thing; it is better to drop down to the object-oriented layer.

@dmikkili
Copy link
Author

I tried the modified script you wrote with the WXAgg backend, and it worked like a charm. You are right though, we did want to do if for long running calculations, and when i implemented it like the example above it was still freezing. I did however manage to get it to not freeze by just adding plt.pause(1) in my for loop to pause between the interactive updates to the plot. That managed to do the trick. I understand though that it isnt the best and will need to rely on something other than pyplot in the future for doing this sort of work. Thanks for all the help.

@mdboom
Copy link
Member

mdboom commented Aug 26, 2013

@efiring: So, at the end of the day, should we close this issue? I think reorganizing it to not rely so much on IPython is worthy of a MEP in and of itself. (I could make a MEP placeholder for it, so we don't lose track).

@efiring
Copy link
Member

efiring commented Aug 26, 2013

@mdboom: I am happy to close it. As for a MEP: given all the backends and possible environments and circumstances in which they might be used, I think we should be cautious about trying to make all combinations work. A good first step would be some perhaps brief improvements in the documentation that we could point to when questions arise. A second step would be a concise single description of the problems to be solved. Then we might be able to see where progress can be made without getting ourselves lost in the GUI mess.

@efiring efiring closed this as completed Aug 26, 2013
@bmorris3
Copy link

@efiring Would you mind pointing me to some documentation that explains what you mean when you say "Generally, the pyplot interface is not suitable for this sort of thing; it is better to drop down to the object-oriented layer." What is the "object-oriented layer"?

@bmorris3
Copy link

Thanks for the informative links @tacaswell , but I don't see how they helps to make sense of @efiring 's comment.

@efiring
Copy link
Member

efiring commented Aug 26, 2013

@bmorris3
In GUI programming, there needs to be one master that keeps track of all the parts and runs the mainloop which responds to events via callbacks. Pyplot provides this functionality, in cooperation with ipython. When one tries to embed pyplot code in an external gui program, however (which I think is what the OP was doing), the external program needs to be the master. The clean solution to this potential conflict (battling masters) is to use the lower-level, non-pyplot part of mpl--the object-oriented component library, and let the real master, the top-level gui program, handle all parts explicitly.

@WeatherGod
Copy link
Member

Another way to look at it is that the different GUI back ends are GUI
programs in their own rights, just a very stripped down one. These GUI back
ends play the same role that any GUI you would use for embedding. Pyplot
makes this "embedding" into the back end guis completely painless and
hidden from the user.

So, the OO layer is a deeper layer in matplotlib that gives complete
control to the user, at the sacrifice of convenience.

I hope that helps!
Ben Root

@keon
Copy link

keon commented May 10, 2015

for anyone find this link on google, (which i did)
http://stackoverflow.com/questions/3441874/matplotlib-animation-either-freezes-after-a-few-frames-or-just-doesnt-work

link above will help

kevin-keraudren pushed a commit to kevin-keraudren/chanvese that referenced this issue Mar 16, 2016
wdvr added a commit to wdvr/morphsnakes that referenced this issue Apr 26, 2016
mrakgr added a commit to mrakgr/Python-Projects that referenced this issue Jan 6, 2019
…ocked-up.

1:15pm. matplotlib/matplotlib#2134

Hmmm, yeah, this is it, but what do I do about it? What does dropping down to the OO layer mean?

https://stackoverflow.com/questions/3441874/matplotlib-animation-either-freezes-after-a-few-frames-or-just-doesnt-work

Ah, what a bother. Can't one just messages to a async event loop?

Well it does not matter, let me just go through the example.

1:30pm. Ah, I am missing the `torchvision` module.

Hmmm...how did I install PyTorch again. Did I run VS in administrator mode and then use the IDE or was it something else?

Well, let me give that a shot first.

1:45pm. I am getting a broken pipe error. This is probably the IDE got a really old version of `torchvision`. I guess my memory of having to run as administrator and then from the shell was right.

I'll have to wait a bit for 700mb to download.

2pm. The pipe is still broken. Well, nevermind. Let me move to the next tutorial.

I'll eventually have to figure out what all of this is doing under the hood, but for the next few days I think just going through these tutorials step by step should be fine."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants