使用matplotlib给我以下警告:"UserWarning:tight_layout:

Pet*_*hao 4 python graph matplotlib

尝试使用python matplotlib做一个图:但继续得到以下waring消息:

"UserWaring: tight_layout: falling back to Agg renderer  warnings.warn("tight_layout: falling back to Agg renderer")
Run Code Online (Sandbox Code Playgroud)

我的代码如下:

plt.legend(loc='upper left',prop = {'size':7},bbox_to_anchor=(1,1))
plt.tight_layout(pad=7)
plt.xlabel ('Build')
plt.ylabel ('Time/Sec')
plt.title ('Performance Test')
plt.grid()
plt.show()
Run Code Online (Sandbox Code Playgroud)

代码图 如何修复该警告信息?

小智 11

你在使用MacOSX吗?这似乎是一个众所周知的问题

https://github.com/matplotlib/matplotlib/issues/1852

我建议重新组织代码,以便使用Figure而不是pyplot.您可以从plt.figure()方法获取图.然后,在Figure实例上调用set_tight_layout(True).试试这个示例代码:

import matplotlib
matplotlib.use('pdf')
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
fig.set_tight_layout(True)
fig.savefig('asd.pdf')  # No warning now
Run Code Online (Sandbox Code Playgroud)

作为旁注,请看一下matplotlib文档

tight_layout()可以获取pad,w_pad和h_pad的关键字参数.它们控制图形边界周围和子图之间的额外填充.焊盘以字体大小的分数指定.

http://matplotlib.org/users/tight_layout_guide.html

这表明你的代码

plt.tight_layout(pad=7)

是错误的,因为pad的值应该在0和1之间.