7

Consider the following simple code:

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0,np.pi,0.001)
f = np.sin(x)

plt.figure(figsize=(10,10))
plt.plot(x,f)
plt.ioff()
plt.show()
plt.savefig('Sine')

Here, I want Python to save the figure after I closed the figure window. Of course, this isn't very useful, but in the original code I want to manipulate the figure graphically and then save the changes.

The above code worked fine with my last Python version (Version 2.? with Debian), but since I changed to SuSe 13.2 with Python 3.4 it simply runs the whole code without a stop.

There do exist other threads on this topic like Matplotlib python show() returns immediately but those solutions don't work for me - I tried matplotlib.interactive(False) and choosing various different backends for matplotlib (currently I'm using 'QT4Agg').

Community
  • 1
  • 1
phlegmax
  • 419
  • 1
  • 4
  • 11
  • What happers when you move`plt.ioff()` before `plt.figure`? In principle, you should call it before you create figures and maybe matplotlib for Python 3.4 is more strict on that. – oschoudhury Apr 14 '15 at 14:15
  • Unfortunately, this doesn't help either. I even put matplotlib.interactive(False) in the header, without any effect. – phlegmax Apr 14 '15 at 20:29
  • Are you running this code within an IPython session? You should mention this in your question. – ali_m Apr 16 '15 at 12:21

1 Answers1

6

Please use the Agg backend. It is non interactive and it solves this issue. To set a matplotlib backend you need to call use method as shown below.

import matplotlib
matplotlib.use('Agg')
Tejus Prasad
  • 6,322
  • 7
  • 47
  • 75
  • 4
    Probably not current anymore: `UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.` – oarfish Mar 24 '22 at 15:07