2016-08-25 66 views
1

我是編程和Python的新手,所以請諒解,如果這是一個愚蠢的錯誤。Python 2.7:TypeError:'float'對象沒有屬性'__getitem__'

我試圖運行一個腳本,我想基於對數正態分佈的樣本數據,然後繪製數據的直方圖。

我不斷收到錯誤

這裏是我的代碼:

import numpy as np 
import matplotlib.pyplot as plt 

a = 0.75 + (1.25 - 0.75)*np.random.lognormal(10000) 

[n,bins,patches] = plt.hist(a, bins=50, color = 'red',alpha = 0.5, normed = True) 

plt.show() 

錯誤:

Traceback (most recent call last): 
File "H:\UQ&M\GUI Demos\WIP\Tester.py", line 10, in <module> 
[n,bins,patches] = plt.hist(a, bins=50, color = 'red',alpha = 0.5, normed = True) 
File "C:\Program Files (x86)\python27\lib\site-packages\matplotlib\pyplot.py", line 2341, in hist 
ret = ax.hist(x, bins, range, normed, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, **kwargs) 
File "C:\Program Files (x86)\python27\lib\site-packages\matplotlib\axes.py", line 7650, in hist 
if isinstance(x, np.ndarray) or not iterable(x[0]): 
TypeError: 'float' object has no attribute '__getitem__' 

我已經在這裏閱讀過類似疑問的,但是我似乎無法找到一個解決方案。

您的專家建議將不勝感激。

預先感謝把你寶貴的時間尋找到這一點。

+3

'一== inf',一個簡單的標量。 'hist(x,...)'需要一個序列作爲第一個參數。您可能想要'a = 0.75 +(1.25 - 0.75)* np.random.lognormal(size = 10000)' – dhke

+0

@dhke:您可能想要將其作爲答案 –

+0

@AndreaCorbellini Nah,印刷錯誤,近距離投票;) – dhke

回答

0

matplotlib API a應該是一個數組或序列,如果我就在你的代碼,它是一個單一的數字不是一個數組

相關問題