from macslib import * from random import * import numpy as np # Change barWidth, numBars, numRepetitions, and numSamples to see how it affects the histogram created! heights = loadData('heightInches.csv') barWidth = 0.1 numBars = 50 # Finds the mean height so that half of the histogram bars # are on the left and half of the bars are on the right sumHeights = 0.0 for val in heights: sumHeights = sumHeights + val meanHeight = sumHeights/len(heights) barIndexOffset = int(meanHeight/barWidth - numBars/2) # Create the text that is displayed on the x axis xTicksPos = np.arange(numBars) xTicksTxt = xTicksPos*barWidth + barIndexOffset*barWidth frequency = [0] * numBars numRepetitions = 100 for rep in range(numRepetitions): numSamples = 75 sampledHeights = []; for ct in range(numSamples): sampledHeights.append( choice( heights ) ) sumSampledHeights = 0.0 for val in sampledHeights: sumSampledHeights = sumSampledHeights + val meanSampledHeights = sumSampledHeights/len(sampledHeights) barIndex = int(meanSampledHeights/barWidth - barIndexOffset) frequency[barIndex] = frequency[barIndex] + 1 drawHistogram( frequency ) plt.xticks(xTicksPos+0.4,xTicksTxt,rotation='vertical')