def plotDiffs(sampleSizes, diffs, title, label, color = 'b'): numpy.plot(sampleSizes, diffs, label = label, color = color) numpy.xlabel('Sample Size') numpy.ylabel('% Difference in SD') numpy.title(title) numpy.legend()
def compareAnimals(animals, precision): """Assumes animals is a list of animals, precision an int >= 0 Builds a table of Euclidean distance between each animal""" #Get labels for columns and rows columnLabels = [] for a in animals: columnLabels.append(a.getName()) rowLabels = columnLabels[:] tableVals = [] #Get distances between pairs of animals #For each row for a1 in animals: row = [] #For each column for a2 in animals: if a1 == a2: row.append('--') else: distance = a1.distance(a2) row.append(str(round(distance, precision))) tableVals.append(row) #Produce table table = numpy.table(rowLabels = rowLabels, colLabels = columnLabels, cellText = tableVals, cellLoc = 'center', loc = 'center', colWidths = [0.2]*len(animals)) table.scale(1, 2.5) numpy.title('Eucliedan Distance Between Animals')
def testFits(models, degrees, xVals, yVals, title): numpy.plot(xVals, yVals, 'o', label='Data') for i in range(len(models)): estYVals = numpy.polyval(models[i], xVals) error = rSquared(yVals, estYVals) numpy.plot(xVals, estYVals, label = 'Fit of degree '\ + str(degrees[i])\ + ', R2 = ' + str(round(error, 5))) numpy.legend(loc='best') numpy.title(title)
def make_one_curve_plot(x_coords, y_coords, x_label, y_label, title): """ Makes a plot of the x coordinates and the y coordinates with the labels and title provided. Args: x_coords (list of floats): x coordinates to graph y_coords (list of floats): y coordinates to graph x_label (str): label for the x-axis y_label (str): label for the y-axis title (str): title for the graph """ pl.figure() pl.plot(x_coords, y_coords) pl.xlabel(x_label) pl.ylabel(y_label) pl.title(title) pl.show()
def fitData(fileName): xVals, yVals = getData(fileName) xVals = numpy.array(xVals) yVals = numpy.array(yVals) xVals = xVals*9.81 #get force numpy.plot(xVals, yVals, 'bo', label = 'Measured points') model = numpy.polyfit(xVals, yVals, 1) xVals = xVals + [2] yVals = yVals + [] estYVals = numpy.polyval(model, xVals) numpy.plot(xVals, estYVals, 'r', label = 'Linear fit, r**2 = ' + str(round(rSquared(yVals, estYVals), 5))) model = numpy.polyfit(xVals, yVals, 2) estYVals = numpy.polyval(model, xVals) numpy.plot(xVals, estYVals, 'g--', label = 'Quadratic fit, r**2 = ' + str(round(rSquared(yVals, estYVals), 5))) numpy.title('A Linear Spring') labelPlot() numpy.legend(loc = 'best')
def show_plot_compare_strategies(title, x_label, y_label): """ Produces a plot comparing the two robot strategies in a 20x20 room with 80% minimum coverage. """ num_robot_range = range(1, 11) times1 = [] times2 = [] for num_robots in num_robot_range: print("Plotting", num_robots, "robots...") times1.append( run_simulation(num_robots, 1.0, 1, 20, 20, 3, 0.8, 20, StandardRobot)) times2.append( run_simulation(num_robots, 1.0, 1, 20, 20, 3, 0.8, 20, FaultyRobot)) numpy.plot(num_robot_range, times1) numpy.plot(num_robot_range, times2) numpy.title(title) numpy.legend(('StandardRobot', 'FaultyRobot')) numpy.xlabel(x_label) numpy.ylabel(y_label) numpy.show()
def show_plot_room_shape(title, x_label, y_label): """ Produces a plot showing dependence of cleaning time on room shape. """ aspect_ratios = [] times1 = [] times2 = [] for width in [10, 20, 25, 50]: height = 300 / width print("Plotting cleaning time for a room of width:", width, "by height:", height) aspect_ratios.append(float(width) / height) times1.append( run_simulation(2, 1.0, 1, width, height, 3, 0.8, 200, StandardRobot)) times2.append( run_simulation(2, 1.0, 1, width, height, 3, 0.8, 200, FaultyRobot)) numpy.plot(aspect_ratios, times1) numpy.plot(aspect_ratios, times2) numpy.title(title) numpy.legend(('StandardRobot', 'FaultyRobot')) numpy.xlabel(x_label) numpy.ylabel(y_label) numpy.show()
def make_two_curve_plot(x_coords, y_coords1, y_coords2, y_name1, y_name2, x_label, y_label, title): """ Makes a plot with two curves on it, based on the x coordinates with each of the set of y coordinates provided. Args: x_coords (list of floats): the x coordinates to graph y_coords1 (list of floats): the first set of y coordinates to graph y_coords2 (list of floats): the second set of y-coordinates to graph y_name1 (str): name describing the first y-coordinates line y_name2 (str): name describing the second y-coordinates line x_label (str): label for the x-axis y_label (str): label for the y-axis title (str): the title of the graph """ pl.figure() pl.plot(x_coords, y_coords1, label=y_name1) pl.plot(x_coords, y_coords2, label=y_name2) pl.legend() pl.xlabel(x_label) pl.ylabel(y_label) pl.title(title) pl.show()
def showErrorBars(population, sizes, numTrials): xVals = [] sizeMeans, sizeSDs = [], [] for sampleSize in sizes: xVals.append(sampleSize) trialMeans = [] for t in range(numTrials): sample = random.sample(population, sampleSize) popMean, sampleMean, popSD, sampleSD =\ getMeansAndSDs(population, sample) trialMeans.append(sampleMean) sizeMeans.append(sum(trialMeans)/len(trialMeans)) sizeSDs.append(numpy.std(trialMeans)) print(sizeSDs) numpy.errorbar(xVals, sizeMeans, yerr = 1.96*numpy.array(sizeSDs), fmt = 'o', label = '95% Confidence Interval') numpy.title('Mean Temperature (' + str(numTrials) + ' trials)') numpy.xlabel('Sample Size') numpy.ylabel('Mean') numpy.axhline(y = popMean, color ='r', label = 'Population Mean') numpy.xlim(0, sizes[-1] + 10) numpy.legend()
def labelPlot(): numpy.title('Measured Displacement of Spring') numpy.xlabel('|Force| (Newtons)') numpy.ylabel('Distance (meters)')
except: years[d.getYear()] = [d.getHigh()] for y in years: years[y] = sum(years[y])/len(years[y]) return years data = getTempData() years = getYearlyMeans(data) xVals, yVals = [], [] for e in years: xVals.append(e) yVals.append(years[e]) numpy.plot(xVals, yVals) numpy.xlabel('Year') numpy.ylabel('Mean Daily High (C)') numpy.title('Select U.S. Cities') def splitData(xVals, yVals): toTrain = random.sample(range(len(xVals)), len(xVals)//2) trainX, trainY, testX, testY = [],[],[],[] for i in range(len(xVals)): if i in toTrain: trainX.append(xVals[i]) trainY.append(yVals[i]) else: testX.append(xVals[i]) testY.append(yVals[i]) return trainX, trainY, testX, testY
def makeHist(data, title, xlabel, ylabel, bins = 20): numpy.hist(data, bins = bins) numpy.title(title) numpy.xlabel(xlabel) numpy.ylabel(ylabel)
popSD = numpy.std(population) sems = [] sampleSDs = [] for size in sampleSizes: sems.append(sem(popSD, size)) means = [] for t in range(numTrials): sample = random.sample(population, size) means.append(sum(sample)/len(sample)) sampleSDs.append(numpy.std(means)) numpy.plot(sampleSizes, sampleSDs, label = 'Std of ' + str(numTrials) + ' means') numpy.plot(sampleSizes, sems, 'r--', label = 'SEM') numpy.xlabel('Sample Size') numpy.ylabel('Std and SEM') numpy.title('SD for ' + str(numTrials) + ' Means and SEM') numpy.legend() def plotDistributions(): uniform, normal, exp = [], [], [] for i in range(100000): uniform.append(random.random()) normal.append(random.gauss(0, 1)) exp.append(random.expovariate(0.5)) makeHist(uniform, 'Uniform', 'Value', 'Frequency') numpy.figure() makeHist(normal, 'Gaussian', 'Value', 'Frequency') numpy.figure() numpy.show() makeHist(exp, 'Exponential', 'Value', 'Frequency')