Numpy-histogram-using-matplotlib

提供:Dev Guides
移動先:案内検索

NumPy-Matplotlibを使用したヒストグラム

NumPyには、データの度数分布のグラフィカルな表現である* numpy.histogram()関数があります。 *bin と呼ばれるクラス間隔に対応する水平サイズが等しい長方形と、周波数に対応する*可変高*。

numpy.histogram()

numpy.histogram()関数は、入力配列とビンを2つのパラメーターとして受け取ります。 ビン配列内の連続する要素は、各ビンの境界として機能します。

import numpy as np

a = np.array([22,87,5,43,56,73,55,54,11,20,51,5,79,31,27])
np.histogram(a,bins = [0,20,40,60,80,100])
hist,bins = np.histogram(a,bins = [0,20,40,60,80,100])
print hist
print bins

それは次の出力を生成します-

[3 4 5 2 1]
[0 20 40 60 80 100]

plt()

Matplotlibは、ヒストグラムのこの数値表現をグラフに変換できます。 pyplotサブモジュールの* plt()関数*は、データとビン配列を含む配列をパラメーターとして受け取り、ヒストグラムに変換します。

from matplotlib import pyplot as plt
import numpy as np

a = np.array([22,87,5,43,56,73,55,54,11,20,51,5,79,31,27])
plt.hist(a, bins = [0,20,40,60,80,100])
plt.title("histogram")
plt.show()

それは次の出力を生成する必要があります-

ヒストグラムプロット