解决matplotlib库在PyCharm和命令行都无法正常显示问题

我们在学习人工智能的时候,会经常用到matplotlib,在学习的时候有一些例子写了代码运行:

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(20)
y = [x_i + np.random.rand(1) for x_i in x]
a, b = np.polyfit(x, y, 1)
plt.plot(x, y, 'o', np.arange(20), a*np.arange(20)+b, '-');

点击运行后却无反应

Process finished with exit code 0

其实很简单,只需要加上plt.show()即可。

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(20)
y = [x_i + np.random.rand(1) for x_i in x]
a, b = np.polyfit(x, y, 1)
plt.plot(x, y, 'o', np.arange(20), a*np.arange(20)+b, '-');
plt.show()

image.png

原文地址:https://www.cnblogs.com/taoweiji/p/9028921.html