Making Histograms in Python
The completely free, open source Python add-on libraries Matplotlib and NumPy offer an economic alternative to the commercially available mathematics and statistics programs used for creating graphical representations of numerical information. While these Python tools lack the bells and whistles of the pricey programs, such as an easy-to-use intuitive interface, they also lack the hefty price tags and egregious end-user licensing restrictions. Although they save money, the Python tools will cost a little extra time in installing and creating the Python libraries.
Instructions
-
-
1
Open the terminal application on your Mac or a console window in Windows and type "python -v" at the prompt to ensure that you have Python 2.7; this action tells you the version that you have. If you are using a Mac, the version of Python that came with OS X is not compatible with the add-on libraries that you will have to install; visit Python (python.org) and install Python version 2.7.
-
2
Download and install NumPy and Matplotlib from the Source Forge (sourceforge.net) open-source repository. Binary installers exist for both Mac OS X and Windows operating systems, so you will not have to deal with arcane command line installation procedures.
-
-
3
Open the terminal application in Mac OS X or the console in Windows. Initiate the Python interpreter by typing "Python" at the command line. You will then see the Python prompt. Load the two new libraries with the following "command S":
>>>>import numpy as np
>>>>import matplotlib.pyplot as plt -
4
Create some data for this histogram by defining the axes of the histogram and generating some random IQ scores around a standard distribution with the following commands:
>>>>mu, sigma = 100, 15
>>>>x = mu + sigma * np.random.randn(10000) -
5
Create the layout and parameters of the histogram with the following commands:
n, bins, patches = plt.hist(x, 50, normed=1, facecolor='g', alpha=0.75)Add labels and color the bars, or bins, of the histogram with the following commands:
plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title('Histogram of IQ')
plt.text(60, .025, r'$\mu=100,\ \sigma=15$')
plt.axis([40, 160, 0, 0.03])Finally, generate the histogram with this command:
plt.show()
This will generate a bar graph illustrating IQ scores in the classic bell-curve shape with green bins, with their "y" axis representing IQ scores and their "x" axis representing the number of individuals who reached those scores.
-
1
References
- Photo Credit Hemera Technologies/Photos.com/Getty Images