gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 01:53:43 27 4
gpt4 key购买 nike

尝试使用 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()

graph from the code如何修复该警告消息?

最佳答案

您使用的是 MacOSX 吗?这似乎是一个已知且 Unresolved 问题

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

我建议重新组织代码,以便您使用 Figure 而不是 pyplot。您可以从 plt.figure() 方法获取 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() can take keyword arguments of pad, w_pad and h_pad. These control the extra padding around the figure border and between subplots. The pads are specified in fraction of fontsize.

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

这表明您的代码

plt.tight_layout(pad=7)

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

关于python - 使用 matplotlib 给我以下警告 : "UserWarning: tight_layout:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37309559/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com