13

ヒストグラムを作成するために 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[0]および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)てプロットまたは印刷するXY、予期した曲線/値が得られません。plt.hist私のプロットには部分的なヒストグラムがあるので、それはと関係があると思われます。

4

4 に答える 4

3

np.histogram2d (2D ヒストグラムの場合) またはnp.histogram (1D ヒストグラムの場合)を使用できます。

hst = np.histogram(A, bins)
hst2d = np.histogram2d(X,Y,bins)

plt.hist出力形式はand と同じplt.hist2dですが、唯一の違いはplot がないことです。

于 2020-06-07T09:28:33.380 に答える