数组的分布图 [英] Distribution plot of an array

查看:96
本文介绍了数组的分布图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个numpy数组,其中包含[-10..10]中的浮点值.我想绘制一个值的分布图,像这样(这里是对二项式随机变量完成的):

I have a numpy array containing float values in [-10..10]. I would like to plot a distribution-graph of the values, like this (here it is done for a binomial random variable) :

例如,我希望条形图计算每个间隔[-10,-9.5],[-9.5,-9],...,[9.5,10]中的元素数量.

For example I would like bars counting the number of elements in each interval [-10, -9.5], [-9.5, -9], ..., [9.5, 10].

如何使用Python准备这样的分布图?

How to prepare such a distribution plot with Python?

推荐答案

实际上是matplotlib,在以下位置您将找到与您所追求的代码相对应的代码示例:

Indeed matplotlib, more precisely you'll find samples of code corresponding to what you are after at: http://matplotlib.org/examples/pylab_examples/histogram_demo_extended.html

import numpy as np
import matplotlib.pyplot as plt
mu, sigma = 200, 25
x = mu + sigma*np.random.randn(10000)
n, bins, patches = plt.hist(x)
plt.show()

n包含每个仓中的点数,而bins包含在我的示例中自动生成的截止值.当然,您可以使用plt.hist的选项进行操作以获得所需的图形.

n contains the number of points in each bin and bins the cut off values which are in my example generated automatically. You can of course play with plt.hist's options to obtain the graph that you wish.

在您的情况下,只需将x替换为您的数组,然后使用bins选项进行截取,例如:

In your case, just replace x by your array, and play with the bins option for cut off values e.g.:

plt.hist(x, bins = [-10, -9.5, -9])

您也可以将标量n传递给bins,在这种情况下,plt.hist将确定截断值,以显示带有n bins的漂亮图形.

You can also simlply pass a scalar n to bins in which case plt.hist will determine cut off values to display a nice graph with n bins.

这篇关于数组的分布图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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