我有一组数字作为整数和浮点数,我想从它们绘制直方图。为此,我使用以下代码:
import matplotlib.pyplot as plt
from numpy import array
gn=array([1,2,3,728,625,0,736,5243,9.0])
plt.hist(gn)
plt.show()
但是,我最终收到以下错误:
plt.hist(a)
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 2827, in hist
stacked=stacked, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 8312, in hist
xmin = min(xmin, xi.min())
File "/usr/lib/python2.7/dist-packages/numpy/core/_methods.py", line 21, in _amin
out=out, keepdims=keepdims)
TypeError: cannot perform reduce with flexible type
我不知道我哪里出错了,有人可以建议我如何绘制浮点数和整数的直方图
答案 0 :(得分:1)
有趣。当我运行它时,numpy会自动使整数浮动。必须是不同的版本。您可以使用astype()
方法将数组从flexible更改为float dtype。试试这个:
import matplotlib.pyplot as plt
from numpy import array
gn=array([1,2,3,728,625,0,736,5243,9.0])
plt.hist(gn.astype('float'))
plt.show()