2017-12-02 5 views
2

matplotlibのヒストグラムを使用すると、ビンの数を選択できます。 しかし、どのようにnumpyのヒストグラムでビンの数を選ぶことができますか?numpy.histogramのビン数を選択する方法は?

import matplotlib.pyplot as plt 
import numpy as np 
array = [1,3,4,4,8,9,10,12] 

range = int((max(array)) - min(array))+1 
x, bins, patch = plt.hist(array, bins=range) 
この場合

範囲=ビンの数=(12-1)+1 = 12

その結果は、x = [1 0 1 2 0 0 0 あります。1. 1. 1. 0 1]

しかしnumpyの結果は

hist, bin_edges = np.histogram(array, density=False) 

numpyの= [1 1 2 0 0 0 1 1 1 1] numpy_bin = [1 2.1であります3.2 4.3 5.4 6.5 7.6 8.7 9.8 10.9 12.]

numpyのを使用して、どのように私はビンの数(= INT((MAX(配列)) - 分(配列))+ 1)を選択することができ、私はmatplotlibのように同じ結果が欲しい

+0

'bincount'を使う:' np.bincount(array) '? – Divakar

答えて

0

matplotlibの範囲はあなたが同じ大きさのビンのrange ammountを得る整数である場合

hist, edges = np.histogram(array, bins=range, density=False) 

:単にnp.histogrambins=rangeとしてキーワード引数を追加するビンの数を渡すために、numpysヒストグラムを使用しています。 np.histogramのビンのデフォルト値はbins='auto'で、アルゴリズムを使用してビン数を決定します。続きを読む:https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.histogram.html

array = [1,3,4,4,8,9,10,12] 
range = int((max(array)) - min(array))+1 
x, bins, patch = plt.hist(array, bins=range) 

x 
array([ 1., 0., 1., 2., 0., 0., 0., 1., 1., 1., 0., 1.]) 

hist, edges = np.histogram(array, bins=range) 

hist 
array([1, 0, 1, 2, 0, 0, 0, 1, 1, 1, 0, 1], dtype=int64) 

bins == edges 
array([ True, True, True, True, True, True, True, True, True, 
     True, True, True, True], dtype=bool) 
関連する問題