Matplotlib在图中未显示线

问题描述 投票:0回答:1

[您好,我是Python的新手,我正在阅读一个教程,只是为了了解matplotlib如何绘制图形,而问题在于图形未显示任何线条。有人可以指出问题出在哪里吗?我正在使用Python 3.7.5matplotlib 3.1.1

这是教程链接https://www.youtube.com/watch?v=GIywmJbGH-8&t=109s

代码

import time
import psutil
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
fig.show()

i = 0
x, y = [], []

while True:
    x.append(i)
    y.append(psutil.cpu_percent())

    ax.plot(x, y, color='b')

    fig.canvas.draw()

    ax.set_xlim(left=max(0, i - 50), right=i + 50)

    time.sleep(0.1)
    i += 1

plt.close()

和我得到的输出enter image description here

python matplotlib plot graph pycharm
1个回答
0
投票

正如评论所说,如果在Jupyter Notebook中执行,则可以使用。

虽然通常将所有代码都显示为文本,但我在这里使用的是图片来传达笔记本中的外观。

e

© www.soinside.com 2019 - 2024. All rights reserved.