简体   繁体   中英

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?

@FilipeCorreia在评论中给出的解决方法是删除mpl.use('Agg') ,并使用fig.set_tight_layout(True)代替fig.tight_layout()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM