matplotlib的pyplot简单使用

话不多说,先用它绘制一个 sin 函数的图像:

import numpy as np
import matplotlib.pyplot as plt

# 生成数据
x = np.arange(0, 6, 0.1)  # 以 0.1 为单位,生成 0-6 的数据
y = np.sin(x)

# 绘制图形
plt.plot(x, y)
plt.show()

绘制结果:
pyplot

我们再尝试追加 cos 函数的图形,并尝试添加标题和坐标轴签名:

import numpy as np
import matplotlib.pyplot as plt

# 生成数据
x = np.arange(0, 6, 0.1) # 以0.1为单位,生成0到6的数据
y1 = np.sin(x)
y2 = np.cos(x)

# 绘制图形
plt.plot(x, y1, label="sin")
plt.plot(x, y2, linestyle = "--", label="cos")
plt.xlabel("x") # x轴的标签
plt.ylabel("y") # y轴的标签
plt.title('sin & cos')
plt.legend()
plt.show()

绘制结果:
在这里插入图片描述

还可以用来显示图像:

# coding: utf-8
import matplotlib.pyplot as plt
from matplotlib.image import imread

img = imread('../dataset/lena.png') #读入图像
plt.imshow(img)

plt.show()

运行后可以看到结果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_45668004/article/details/118382687