我想在只有一個繪圖的圖上使用matplotlib的tight_layout函數,但它給了我一個AssertionError。這是我的代碼:(注:更新w.r.t.原帖)matplotlib的tight_layout中的斷言錯誤()
def plot_histogram_00():
fig, _ = plt.subplots(figsize=(9, 5))
fig.tight_layout()
fig.close()
當我在Eclipse我的Windows 7電腦(Python 2.7版,matplotlib 1.4)上運行此,它運行良好,我得到一個不錯的輸出。然而,當我在我的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
就是有人能指出我我將開始尋找這個錯誤的原因?
UPDATE:我原來的代碼是這樣的:
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')
所有這些行都需要觸發錯誤? – Veedrac 2014-10-07 14:01:27
奇怪....還要確保清理你的數字(pyplot保留一個全局列表,'fig.close()'後保存應該足夠) – tacaswell 2014-10-07 16:14:39
確實不是所有的行都需要,情節本身並不是。我更新了原始文章,包括'fig.close()'語句。我仍然觀察到這種行爲(在Windows中工作,不在Mac中工作)。 – physicalattraction 2014-10-08 08:15:12