mercredi 23 octobre 2019

Struggling to make the plot show up on this simple code

I am going through examples from an intro to python book and the following code was given to us, and by calling the function flipPlot1, the plot should show up, but I am not able to get it.

I've made sure everything that needs to be imported, was. Gone over the code multiple times for mistakes. There is no error called from my code.

 def makePlot(x, y, title, xLabel, yLabel, style, logX = False, logY = False):
  pylab.figure()
  pylab.title(title)
  pylab.xlabel(xLabel)
  pylab.ylabel(yLabel)
  pylab.plot(x, y, style)
  if logX:
    pylab.semilogx()
  if logY:
    pylab.semilogy()

def runTrial(numFlips):
  numHeads = 0
  for n in range(numFlips):
    if random.choice(('H', 'T')) == 'H':
        numHeads += 1
  numTails = numFlips - numHeads
  return (numHeads, numTails)

def flipPlot1(minExp, maxExp, numTrials):
  ratiosMeans, diffsMeans, ratiosSDs, diffsSDs = [], [], [], []
  xAxis = []
 for exp in range(minExp, maxExp + 1):
    xAxis.append(2**exp)
 for numFlips in xAxis:
    ratios, diffs = [], []
    for t in range(numTrials):
        numHeads, numTails, = runTrial(numFlips)
        ratios.append(numHeads/numTails)
        diffs.append(abs(numHeads - numTails))
    ratiosMeans.append(sum(ratios)/numTrials)
    diffsMeans.append(sum(diffs)/numTrials)
    ratiosSDs.append(stdDev(ratios))
    diffsSDs.append(stdDev(diffs))
  numTrialsString = ' (' + str(numTrials) + ' Trials)'
  title = 'Mean Heads/Tails Ratios' + numTrialsString

  makePlot(xAxis, ratiosMeans, title, 'Number of Flips', 'Mean Heads/Tails', 'ko', logX = True)

  title = 'SD Heads/Tails Ratios' + numTrialsString
  makePlot(xAxis, ratiosSDs, title, 'Number of Flips', 'Standard Deviation', 'ko', logX = True, logY = True)

flipPlot1(4, 20, 20)

There is no error, but no plot shows up




Aucun commentaire:

Enregistrer un commentaire