Distribution plot of an array

江枫思渺然 提交于 2019-12-22 07:18:08

问题


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) :

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

How to prepare such a distribution plot with Python?


回答1:


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 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.

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])

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.



来源:https://stackoverflow.com/questions/22729019/distribution-plot-of-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!