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

时间:2016-08-25 09:26:47

标签: python python-2.7 python-3.x numpy

我是编程和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__'

我在这里已经阅读了类似的查询,但我似乎无法找到解决方案。

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

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

1 个答案:

答案 0 :(得分:0)

来自matplotlib API $.ajaxSetup({ beforeSend: function(xhr, settings) { function getCookie(name) { var cookieValue = null; if (document.cookie && document.cookie != '') { var cookies = document.cookie.split(';'); for (var i = 0; i < cookies.length; i++) { var cookie = jQuery.trim(cookies[i]); // Does this cookie string begin with the name we want? if (cookie.substring(0, name.length + 1) == (name + '=')) { cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); break; } } } return cookieValue; } if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) { // Only send the token to relative URLs i.e. locally. xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken')); } } }); 应该是一个数组或序列,如果我在你的代码中,它是一个数字而不是一个数组