Maison > Questions et réponses > le corps du texte
我在运行MIT的python公开课所提供的如下一段代码的时候,Spyder IDE报错
NameError: name 'figure' is not defined
同时,IDE在代码部分给出了错误提示,'from pylab import * unable to detect undefined names'
请教各位,该如何处理这一错误,多谢
补充:有没有可能是这个库没有导进去,但是之前我跑类似的代码,导入pylab是可以正常画图的,中间也没有改变Python或者anaconda的设置,怎么会突然不好用了?作为初学者,真的有些手足无措。
from pylab import *
import random, math
def flipTrial(numFlips):
heads, tails = 0, 0
for i in xrange(0, numFlips): # Comparing traditional range,xrange() save more space
coin = random.randint(0, 1)
if coin == 0: heads += 1
else: tails += 1
return heads, tails
def simFlips(numFlips, numTrials):
diffs = []
for i in xrange(0, numTrials):
heads, tails = flipTrial(numFlips)
diffs.append(abs(heads - tails))
diffs = array(diffs)
diffMean = sum(diffs)/len(diffs)
diffPercent = (diffs/float(numFlips))*100
percentMean = sum(diffPercent)/len(diffPercent)
hist(diffs)
axvline(diffMean, color = 'r', label = 'Mean')
legend()
titleString = str(numFlips) + ' Flips, ' + str(numTrials) + ' Trials'
title(titleString)
xlabel('Difference between heads and tails')
ylabel('Number of Trials')
figure()
plot(diffPercent)
axhline(percentMean, color = 'r', label = 'Mean')
legend()
title(titleString)
xlabel('Trial Number')
ylabel('Percent Difference between heads and tails')
simFlips(100,100)
show()