繁体   English   中英

模块'matplotlib.pyplot'没有属性'tight_layout'

[英]module 'matplotlib.pyplot' has no attribute 'tight_layout'

由于某种原因,pyplot中的tight_layout()函数停止在机器上工作。 我已经在我的一个程序中使用它几个月了,它决定停止工作(我意识到我可能在某个偶然的地方进行了更改,造成了这种情况,但我不知道它在哪里)。 任何其他的matplotlib或pyplot函数都可以正常工作,但是每当我调用ight_layout时,我都会

module 'matplotlib.pyplot' has no attribute 'tight_layout'

这显然是不正确的。

例如,即使此代码也来自官方的tight_layout指南( https://matplotlib.org/users/tight_layout_guide.html

from matplotlib import pyplot as plt

plt.rcParams['savefig.facecolor'] = "0.8"

def example_plot(ax, fontsize=12):
     ax.plot([1, 2])
     ax.locator_params(nbins=3)
 ax.set_xlabel('x-label', fontsize=fontsize)
 ax.set_ylabel('y-label', fontsize=fontsize)
 ax.set_title('Title', fontsize=fontsize)

plt.close('all')
fig, ax = plt.subplots()
example_plot(ax, fontsize=24)
plt.tight_layout()

会产生错误。

我已经重新安装了matplotlib软件包两次,并重新安装了我的IDE(Spyder)无济于事。 在这一点上我还很笨,所以任何输入都会很棒。

我认为某些模块不会突然失去其方法。 您可以检查代码中的方法,

import matplotlib.pyplot as plt
with open(plt.__file__[:-1],"r") as f:
    for line in f:
        if "tight_layout" in line:
            print(line)

这将导致打印两行。

def tight_layout(pad=1.08, h_pad=None, w_pad=None, rect=None):
# and 
fig.tight_layout(pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect)

如果不是这种情况,请检查文件的最后更改时间

import os
from datetime import datetime

f = datetime.fromtimestamp(os.path.getmtime(plt.__file__[:-1]))
print("Modification time: {}".format(f))

并与您执行的所有操作进行比较,包括安装,卸载,修改代码等。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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