Histograms

media type="custom" key="22072724"   Home &gt;  Statistics  &gt; Histogram    Example 1 - using matplotlib - minimum code required  my_data=[random for j in range(100)]
 * Keywords:** histograms, statistics, matplotlib,

 import matplotlib.pyplot as plt

 plt.hist(my_data, bins=5, range=(0.,1.))

 plt.savefig('Histogram.png')

 plt.close <span style="display: block; height: 1px; left: -40px; overflow: hidden; position: absolute; top: 64px; width: 1px;">  <font color="#FF0066">Deciphering the code <span style="display: block; height: 1px; left: -40px; overflow: hidden; position: absolute; top: 64px; width: 1px;"> my_data=[random for j in range(100)]   #this generates a list</a> of 100 random numbers between [0,1]

<span style="display: block; height: 1px; left: -40px; overflow: hidden; position: absolute; top: 64px; width: 1px;"> import matplotlib.pyplot as plt                  #this imports the "library" with a useful histogram command

<span style="display: block; height: 1px; left: -40px; overflow: hidden; position: absolute; top: 64px; width: 1px;"> plt.hist(my_data, bins=5, range=(0.,1.))    #this creates a plot of a histogram of the data (more below on the options here)

<span style="display: block; height: 1px; left: -40px; overflow: hidden; position: absolute; top: 64px; width: 1px;"> plt.savefig('Histogram.png')                      #these last 2 lines are related to a bug in matplotlib; they force the plot to display

<span style="display: block; height: 1px; left: -40px; overflow: hidden; position: absolute; top: 64px; width: 1px;"> plt.close

<span style="display: block; height: 1px; left: -40px; overflow: hidden; position: absolute; top: 64px; width: 1px;"> </a> <font color="#006600">Histogram Options <span style="display: block; height: 1px; left: -40px; overflow: hidden; position: absolute; top: 64px; width: 1px;"> <ul style=" margin-top:-3px"> <span style="display: block; height: 1px; left: -40px; overflow: hidden; position: absolute; top: 64px; width: 1px;"> <li>bins         ex. bins=10 is the number of equal, sequential intervals that the data should be divided into by their frequency</li> <span style="display: block; height: 1px; left: -40px; overflow: hidden; position: absolute; top: 64px; width: 1px;"> <li>range       ex. range=(-3.,4.5) is an interval of decimals; default is "slightly" longer than the min and max of the datalist.</li> <span style="display: block; height: 1px; left: -40px; overflow: hidden; position: absolute; top: 64px; width: 1px;"> <li>normed    ex. normed=1 determines whether the frequencies are normed so that the area under the histogram is 0; default is normed=0 </li> <span style="display: block; height: 1px; left: -40px; overflow: hidden; position: absolute; top: 64px; width: 1px;"> <li>facecolor ex. facecolor='red' is the color</a> inside the point (marker); default: '#fec7b8' (something blueish)</li> <span style="display: block; height: 1px; left: -40px; overflow: hidden; position: absolute; top: 64px; width: 1px;"> <li>alpha       ex. alpha=0.5 is the opacity</a> of the facecolor; default: 1 (completely opaque)</li> <span style="display: block; height: 1px; left: -40px; overflow: hidden; position: absolute; top: 64px; width: 1px;"> </ul> <span style="display: block; height: 1px; left: -40px; overflow: hidden; position: absolute; top: 64px; width: 1px;"> Plot Options : I really don't know all the options for plt, i.e. matplotlib. I included some in the SAGE PAGES (links at top).