有什么方法可以使用matplotlib.pyplot创建直方图而无需绘制直方图? [英] Any way to create histogram with matplotlib.pyplot without plotting the histogram?

查看:111
本文介绍了有什么方法可以使用matplotlib.pyplot创建直方图而无需绘制直方图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用matplotlib.pyplot创建直方图.我实际上对这些直方图的图并没有兴趣,但对频率和垃圾箱感兴趣(我知道我可以编写自己的代码来执行此操作,但更喜欢使用此程序包).

I am using matplotlib.pyplot to create histograms. I'm not actually interested in the plots of these histograms, but interested in the frequencies and bins (I know I can write my own code to do this, but would prefer to use this package).

我知道我可以做以下事情,

I know I can do the following,

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].当我尝试使用时会出现问题,

to create a histogram. All I need is freq[0], freq[1], and bins[0]. The problem occurs when I try and use,

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有关,因为我的绘图中有部分直方图.

When I call func(x1,x2,Nbins) and plot or print X and Y, I do not get my expected curve/values. I suspect it something to do with plt.hist, since there is a partial histogram in my plot.

推荐答案

我不知道我是否很好地理解了您的问题,但是在这里,您有一个非常简单的自制直方图示例(以一维表示)或2D),每个都在一个函数中,并且正确地称为:

I don't know if I'm understanding your question very well, but here, you have an example of a very simple home-made histogram (in 1D or 2D), each one inside a function, and properly called:

import numpy as np
import matplotlib.pyplot as plt

def func2d(x, y, nbins):
    histo, xedges, yedges = np.histogram2d(x,y,nbins)
    plt.plot(x,y,'wo',alpha=0.3)
    plt.imshow(histo.T, 
               extent=[xedges.min(),xedges.max(),yedges.min(),yedges.max()],
               origin='lower', 
               interpolation='nearest', 
               cmap=plt.cm.hot)
    plt.show()

def func1d(x, nbins):
    histo, bin_edges = np.histogram(x,nbins)
    bin_center = 0.5*(bin_edges[1:] + bin_edges[:-1])
    plt.step(bin_center,histo,where='mid')
    plt.show()

x = np.random.normal(1.5,1.0, (1000,1000))

func1d(x[0],40)
func2d(x[0],x[1],40)

当然,您可以检查数据居中是否正确,但是我认为该示例显示了有关此主题的一些有用信息.

Of course, you may check if the centering of the data is right, but I think that the example shows some useful things about this topic.

我的建议:尝试避免代码中出现任何循环!他们扼杀了表演.如果您看的话,在我的示例中没有循环. python数值问题的最佳实践是避免循环! Numpy有很多用C实现的功能,可以完成所有的硬循环工作.

My recommendation: Try to avoid any loop in your code! They kill the performance. If you look, In my example there aren't loops. The best practice in numerical problems with python is avoiding loops! Numpy has a lot of C-implemented functions that do all the hard looping work.

这篇关于有什么方法可以使用matplotlib.pyplot创建直方图而无需绘制直方图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆