Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python matplotlib中的断言错误';s紧_布局()_Python_Matplotlib_Runtime Error - Fatal编程技术网

Python matplotlib中的断言错误';s紧_布局()

Python matplotlib中的断言错误';s紧_布局(),python,matplotlib,runtime-error,Python,Matplotlib,Runtime Error,我想在只有一个绘图的图形上使用matplotlib的tight_layout函数,但它给了我一个断言错误。这是我的代码:(注:更新了w.r.t.原始帖子) 当我在我的Windows7计算机(Python2.7,matplotlib 1.4)上的Eclipse中运行这个程序时,它运行得很好,我得到了一个很好的输出。但是,当我在Mac电脑(也是Python 2.7、matplotlib 1.4)上运行完全相同的代码(从Dropbox文件夹运行)时,我收到以下错误消息: Traceback (most

我想在只有一个绘图的图形上使用matplotlib的tight_layout函数,但它给了我一个断言错误。这是我的代码:(注:更新了w.r.t.原始帖子)

当我在我的Windows7计算机(Python2.7,matplotlib 1.4)上的Eclipse中运行这个程序时,它运行得很好,我得到了一个很好的输出。但是,当我在Mac电脑(也是Python 2.7、matplotlib 1.4)上运行完全相同的代码(从Dropbox文件夹运行)时,我收到以下错误消息:

Traceback (most recent call last):
  File "/Users/david/Dropbox/Sandbox/Histogram.py", line 139, in <module>
    plot_histogram_00();
  File "/Users/david/Dropbox/Sandbox/Histogram.py", line 38, in plot_histogram_00
    fig.tight_layout()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/figure.py", line 1654, in tight_layout
    rect=rect)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/tight_layout.py", line 352, in get_tight_layout_figure
    pad=pad, h_pad=h_pad, w_pad=w_pad)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/tight_layout.py", line 131, in auto_adjust_subplotpars
    fig.transFigure.inverted())
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/matplotlib/transforms.py", line 1057, in __init__
    assert isinstance(transform, Transform)
AssertionError

我在mac上使用由管理的matplotlib安装时也遇到了同样的问题。修复方法是在brew中完全删除它(
brew卸载-强制matplotlib
),然后删除
/usr/local/lib/python2.7/site packages
中剩余的源文件夹。从pip重新安装:
pip安装matplotlib


我以前使用的主要原因是matplotlib没有标准的setup.py脚本。由于在最新安装的matplotlib中,这一问题似乎已得到解决,我想我现在可以完全依赖pip了。

触发错误是否需要所有这些行?奇怪。。。。还要确保清理图形(pyplot保留一个全局列表,
fig.close()
保存后就足够了)。实际上,并不需要所有行,例如,绘图本身就不需要了。我更新了原始帖子,包括
fig.close()
语句。我仍然观察到这种行为(在Windows中工作,而不是在Mac中工作)。我无法在Mac上用python 2.7.8、matplotlib 1.4.0重现您的错误。也许你需要在mac上重新安装matplotlib?
Traceback (most recent call last):
  File "/Users/david/Dropbox/Sandbox/Histogram.py", line 139, in <module>
    plot_histogram_00();
  File "/Users/david/Dropbox/Sandbox/Histogram.py", line 38, in plot_histogram_00
    fig.tight_layout()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/figure.py", line 1654, in tight_layout
    rect=rect)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/tight_layout.py", line 352, in get_tight_layout_figure
    pad=pad, h_pad=h_pad, w_pad=w_pad)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/tight_layout.py", line 131, in auto_adjust_subplotpars
    fig.transFigure.inverted())
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/matplotlib/transforms.py", line 1057, in __init__
    assert isinstance(transform, Transform)
AssertionError
import matplotlib
matplotlib.use('TkAgg') 
import matplotlib.pyplot as plt
import numpy as np

def plot_histogram_00():
    xlabels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
               'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
    np.random.seed(1)
    values_A = np.random.choice(np.arange(len(xlabels)), 
                                size=200, replace=True).tolist()
    values_B = np.random.choice(np.arange(len(xlabels)), 
                                size=200, replace=True).tolist()

    fig, ax = plt.subplots(figsize=(9, 5))
    _, bins, patches = plt.hist([values_A, values_B], normed=1,
                                bins=len(xlabels),
                                color=['#3782CC', '#AFD5FA'],
                                label=['A', 'B'])

    fig.tight_layout()
    plt.savefig('my_plot_00.png')