在没有Pylab的情况下创建Python直方图 [英] Creating a Python Histogram without Pylab

查看:106
本文介绍了在没有Pylab的情况下创建Python直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须生成一个具有高斯分布的随机数列表(我能够做到这一点),然后获取这些数并将其绘制在直方图中.我的问题是我应该在不使用pylab(或与此相关的任何其他软件包)中使用内置直方图功能的情况下执行此操作,而我完全不知所措.我一直在网上查看,但找不到任何可以解释我将如何处理的内容,你们中的任何人都知道我能做什么吗?预先感谢.

I have to generate a list of random numbers with a gaussian distribution (I'm able to do this) and then take those numbers and plot them in a histogram. My problem is that I'm supposed to do this without using the built-in histogram function within pylab (or any other package for that matter) and I'm at a complete loss. I've been looking on-line and I haven't found anything that explains how I would go about this, does any of you know what I could do? Thanks in advance.

推荐答案

假设您有一个表示随机数的numpy数组

Let's assume you have a numpy array that represents your random numbers

        rnd_numb=array([ 0.48942231,  0.48536864,  0.48614467, ...,  0.47264172,
    0.48309697,  0.48439782])

要创建直方图,您只需要对数据进行装箱.因此,让我们创建一个定义合并的数组

In order to create a histogram you only need to bin your data. So let's create an array that defines the binning

       bin_array=linspace(0,1,100)

在这种情况下,我们将创建100个线性间隔的bin,范围为0到1

In this case we're creating 100 linearly spaced bins in the range 0 to 1

现在,要创建直方图,您只需执行

Now, in order to create the histogram you can simply do

  my_histogram=[]
  for i in range(len(bin_array)-1):
     mask = (rnd_numb>=bin_array[i])&(rnd_numb<bin_array[i+1])
     my_histogram.append(len(rnd_numb[mask]))

这将创建一个列表,其中包含每个bin中的计数.最后,如果您想可视化直方图,则可以

This creates a list that contains the counts in each bin. Lastly, if you want to visualize your histogram you can do

 plot ((bin_array[1:]+bin_array[:-1])/2.,my_histrogram)

您也可以尝试stepbar.

这篇关于在没有Pylab的情况下创建Python直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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