Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put Python 3.4 matplotlib in non-interactive mode?

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').

like image 234
phlegmax Avatar asked Apr 14 '15 06:04

phlegmax


People also ask

What is matplotlib use (' AGG ')?

The last, Agg, is a non-interactive backend that can only write to files. It is used on Linux, if Matplotlib cannot connect to either an X display or a Wayland display.

Are matplotlib plots interactive?

And with no additional code and only using the simple matplotlib code, the output is an interactive plot where you can zoom in/out, pan it and reset to the original view.

Why is %Matplotlib inline?

Why matplotlib inline is used. You can use the magic function %matplotlib inline to enable the inline plotting, where the plots/graphs will be displayed just below the cell where your plotting commands are written. It provides interactivity with the backend in the frontends like the jupyter notebook.

Can we use matplotlib in Python idle?

Download the “Install Matplotlib (for Mac)” file from my web site, and double-click it to run it. Test your installation. Start IDLE, type “import matplotlib”, and confirm that this command completes without an error.


1 Answers

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')
like image 104
Tejus Prasad Avatar answered Oct 01 '22 21:10

Tejus Prasad