-
Book Overview & Buying
-
Table Of Contents
Sage Beginner's Guide
By :
matplotlib has a built-in function for making histograms, which are used to visualize the distribution of values in a set of data. In this example, we will generate an array of random numbers that are drawn from a Gaussian distribution:
import numpy
import matplotlib.pyplot as plt
data = numpy.random.normal(0, 1, size=1000)
plt.figure(figsize=(4, 4))
plt.hist(data, normed=True, facecolor=(0.9, 0.9, 0.9))
plt.savefig('Histogram.png')
plt.close()The result should be similar to the following plot. Because we are generating random data, your plot will not look exactly like this one:

We
used the hist
function to visualize the distribution of values in an array of pseudo-random numbers. hist requires one argument, which is an array containing the data. In practice, the data would typically consist of experimental measurements, sensor data, or the results of a Monte Carlo simulation. We used the optional argument normed=True to indicate...
Change the font size
Change margin width
Change background colour