繁体   English   中英

Python 2.7:TypeError:'float'对象没有属性'__getitem__'

[英]Python 2.7: TypeError: 'float' object has no attribute '__getitem__'

我是编程和Python的新手,所以请原谅这是一个愚蠢的错误。

我正在尝试运行一个脚本,我想根据对数正态分布生成样本数据,然后绘制该数据的直方图。

我一直在收到错误

这是我的代码:

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__'

我在这里读过类似的问题但是我似乎无法找到解决方案。

您的专家建议将受到高度赞赏。

提前感谢您花费宝贵的时间来研究这个问题。

matplotlib API a应该是一个数组或序列,如果我在你的代码中,它是一个数字而不是一个数组

暂无
暂无

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

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