Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python matplotlib Agg vs. interactive plotting and tight_layout

If I use the Agg backend, I'm unable to keep image windows open with show() (regardless of block=True or not)---they just close virtually immediately. If I don't use Agg, then I get the warning:

/Library/Python/2.7/site-packages/matplotlib-1.2.0-py2.7-macosx-10.8-intel.egg/matplotlib/tight_layout.py:225: UserWarning: tight_layout : falling back to Agg renderer warnings.warn("tight_layout : falling back to Agg renderer")

Sample code:

import matplotlib as mpl
mpl.use('Agg')      # With this line = figure disappears; without this line = warning
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111)
mu, sigma = 0, 0.5
x = np.linspace(-3, 3, 100)
plt.plot(x, mlab.normpdf(x, mu, sigma))
fig.tight_layout()
plt.show()

Is there a different backend or methodology I should be using?

like image 259
DilithiumMatrix Avatar asked Mar 16 '13 21:03

DilithiumMatrix


People also ask

What does matplotlib use (' AGG ') do?

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.

What is Tight_layout matplotlib?

tight_layout automatically adjusts subplot params so that the subplot(s) fits in to the figure area. This is an experimental feature and may not work for some cases. It only checks the extents of ticklabels, axis labels, and titles.


2 Answers

The workaround, given by @FilipeCorreia in a comment, is to remove mpl.use('Agg'), and use fig.set_tight_layout(True) instead of fig.tight_layout().

like image 58
DilithiumMatrix Avatar answered Oct 07 '22 16:10

DilithiumMatrix


Agg is a non-interactive backend, meaning it won't display on the screen, only save to files. Which backend are you using? You have OSX, perhaps you can try the 'macosx', or an interactive backend that uses Agg (eg. QT4Agg, WXAgg).

like image 39
tiago Avatar answered Oct 07 '22 16:10

tiago