tstk

 

需要的库:matplotlib

用法:

import matplotlib.pyplot as plt
plt.plot(x, y) #x, y是两个列表
plt.show()

例子:

import matplotlib.pyplot as plt
x = [1, 2, 3]
y = [2, 3, 4]
plt.plot(x, y) #x, y是两个列表
plt.show()

效果如下:

 
 
实际上,x, y的点越多,图像越精确,我们可以用numpy库生成方便的x,y
当点数量不足的时候,图像可能大相径庭
比如
import matplotlib.pyplot as plt
import numpy as np
import math
x = np.linspace(-100, 100, 20)   #分别代表最小,最大,数量, 生成一个等差数列
y = [math.sin(t) for t in x]
plt.plot(x, y)
plt.show()

本意是输出 y = sin(x) 的图像, 实际上的结果是:

 

当我们把点数量增加到1000

import matplotlib.pyplot as plt
import numpy as np
import math
x = np.linspace(-10, 10, 1000)
y = [math.sin(t) for t in x]
plt.plot(x, y)
plt.show()

 

效果:

 

我们可以改变y的取值生成各种函数图像。

 

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2021-10-15
  • 2021-05-03
  • 2021-11-12
  • 2021-10-27
  • 2021-10-12
  • 2022-12-23
猜你喜欢
  • 2021-12-21
  • 2022-01-26
  • 2021-04-11
  • 2021-10-19
  • 2022-12-23
  • 2021-08-19
  • 2022-12-23
相关资源
相似解决方案