【问题标题】:using matplotlib giving me the following warning: "UserWarning: tight_layout:使用 matplotlib 给我以下警告:“UserWarning:tight_layout:
【发布时间】:2016-09-15 12:39:19
【问题描述】:

尝试使用 python matplotlib 绘制图形:但不断收到以下警告消息:

"UserWaring: tight_layout: falling back to Agg renderer  warnings.warn("tight_layout: falling back to Agg renderer")

我的代码如下:

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

如何修复该警告消息?

【问题讨论】:

  • 能否请您使用代码格式(ctrl + k)并提供适当的数据集来重现警告?

标签: python matplotlib graph


【解决方案1】:

您使用的是 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

附带说明,请查看 matplotlib 文档

tight_layout() 可以接受 pad、w_pad 和 h_pad 的关键字参数。这些控制图形边界周围和子图之间的额外填充。填充以字体大小的分数指定。

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

这表明你的代码

plt.tight_layout(pad=7)

错了,pad的值应该在0到1之间。

【讨论】:

    猜你喜欢
    • 2019-03-23
    • 1970-01-01
    • 2014-10-07
    • 2019-07-17
    • 1970-01-01
    • 2021-07-19
    • 2019-06-19
    • 1970-01-01
    • 2015-12-04
    相关资源
    最近更新 更多