DilithiumMatrix
DilithiumMatrix

Reputation: 18657

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?

Upvotes: 9

Views: 20871

Answers (2)

DilithiumMatrix
DilithiumMatrix

Reputation: 18657

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

Upvotes: 8

tiago
tiago

Reputation: 23502

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

Upvotes: 6

Related Questions