问题所在
import matplotlib.pyplot as plt
#很容易漏掉pyplot ,直接写成 import matplotlib as plt导致错误
错误写法
import numpy as np
import matplotlib as plt
# 随机漫步
position=0
walk=[position]
steps=1000
for i in range(steps):
step= 1 if np.random.randint(0,2) else -1
position+=step
walk.append(position)
plt.plot(walk)
正确写法
import numpy as np
import matplotlib.pyplot as plt
# 随机漫步
position=0
walk=[position]
steps=1000
for i in range(steps):
step= 1 if np.random.randint(0,2) else -1
position+=step
walk.append(position)
plt.plot(walk)