我正在使用python v3.6与pycharm和anaconda。我试着运行下面的代码来绘制一个简单的正弦波;
import numpy as np
import matplotlib.pyplot as plt
# Generate a sequence of numbers from -10 to 10 with 100 steps in between
x = np.linspace(-10, 10, 100)
# Create a second array using sine
y = np.sin(x)
# The plot function makes a line chart of one array against another
plt.plot(x, y, marker="x")
pass
代码运行顺畅,没有错误,但没有出现情节。如何让情节出现?
答案 0 :(得分:4)
你最后错过了plt.show()
。
import numpy as np
import matplotlib.pyplot as plt
# Generate a sequence of numbers from -10 to 10 with 100 steps in between
x = np.linspace(-10, 10, 100)
# Create a second array using sine
y = np.sin(x)
# The plot function makes a line chart of one array against another
plt.plot(x, y, marker="x")
plt.show()
pass
或者,如果要将其保存到文件中
plt.savefig("my_file.png")