欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【Python-BUG】module ‘matplotlib‘ has no attribute ‘plot‘

程序员文章站 2022-03-18 21:37:58
...

问题所在

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)