[英]Plotting histogram of floating point numbers and integers in python
我有一組數字,它們是整數和浮點數,我想根據它們繪制直方圖。 為此,我使用以下代碼:
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
我不知道我要去哪里錯,有人可以建議我如何繪制浮點數和整數的直方圖
有趣。 當我運行此命令時,numpy會自動使整數浮點數。 必須是其他版本。 您可以使用astype()
方法將數組從flexible更改為float astype()
。 嘗試以下方法:
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.