私は、matplotlib.pyplotを使ってヒストグラムを作成しています。私は実際にこれらのヒストグラムのプロットに興味はありませんが、周波数とビンに興味があります(私はこれを行うための独自のコードを書くことができますが、このパッケージを使用することを好むでしょう)。ヒストグラムをプロットせずにmatplotlib.pyplotでヒストグラムを作成する方法はありますか?
私は、ヒストグラムを作成するために、
import numpy as np
import matplotlib.pyplot as plt
x1 = np.random.normal(1.5,1.0)
x2 = np.random.normal(0,1.0)
freq, bins, patches = plt.hist([x1,x1],50,histtype='step')
を以下のことを行うことができます知っています。私が必要とするのは,freq[1]
、bins[0]
です。試してみると問題が発生しますが、機能には
freq, bins, patches = plt.hist([x1,x1],50,histtype='step')
があります。例えば、
def func(x, y, Nbins):
freq, bins, patches = plt.hist([x,y],Nbins,histtype='step') # create histogram
bincenters = 0.5*(bins[1:] + bins[:-1]) # center bins
xf= [float(i) for i in freq[0]] # convert integers to float
xf = [float(i) for i in freq[1]]
p = [ (bincenters[j], (1.0/(xf[j] + yf[j])) for j in range(Nbins) if (xf[j] + yf[j]) != 0]
Xt = [j for i,j in p] # separate pairs formed in p
Yt = [i for i,j in p]
Y = np.array(Yt) # convert to arrays for later fitting
X = np.array(Xt)
return X, Y # return arrays X and Y
私はfunc(x1,x2,Nbins)
、プロットまたは印刷X
とY
を呼んで、私は私の予想される曲線/値を得ることはありません。私はプロットに部分的なヒストグラムがあるので、plt.hist
と何か関係があります。
なぜnp.histogram()を使用しないのですか? – Pablo
提案していただきありがとうございます。それはどこに問題があるかのように見えます。上記のコードを(関数ではなく)行ごとに実行すると、np.histogram()とplt.hist()の両方で動作します。関数でこれを使用する理由についてのアイデアはうまくいきませんか? – user1175720