我有一组整数和 float 字,我想根据它们绘制直方图。为此,我使用以下代码:
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
我不知道我哪里出错了,有人可以建议我如何绘制 float 和整数的直方图
最佳答案
有趣。当我运行这个时,numpy 会自动使整数 float 。一定是不同的版本。您可以使用 astype()
方法将数组从灵活数据类型更改为 float 据类型。试试这个:
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()
关于python - 在python中绘制 float 和整数的直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32258654/