python matplotlib.pyplot 显示图像

1. example1

import cv2
import matplotlib.pyplot as plt

img1 = cv2.imread('/media/data_2/everyday/0930/1.jpeg')
img2 = cv2.imread('/media/data_2/everyday/0930/2.jpg')
img3 = cv2.imread('/media/data_2/everyday/0930/3.jpeg')
img4 = cv2.imread('/media/data_2/everyday/0930/4.jpeg')

img1 = cv2.resize(img1,(800,800))
img2 = cv2.resize(img2,(800,800))
img3 = cv2.resize(img3,(800,800))
img4 = cv2.resize(img4,(800,800))

# img[:, :, ::-1]是将BGR转化为RGB
plt.subplot(141) #要生成1行4列,这是第一个图plt.subplot('行','列','编号')
plt.imshow(img1[:, :, ::-1])  
plt.title('dog-1')
plt.subplot(142)
plt.imshow(img2[:, :, ::-1]) 
plt.title('dog-2')

plt.subplot(143)
plt.imshow(img3[:, :, ::-1])
plt.title('dog-3') 
plt.subplot(144)
plt.imshow(img4[:, :, ::-1]) 
plt.title('cat-1')
plt.show()

2. example2

import cv2
import matplotlib.pyplot as plt

img1 = cv2.imread('/media/data_2/everyday/0930/1.jpeg')
img2 = cv2.imread('/media/data_2/everyday/0930/2.jpg')
img3 = cv2.imread('/media/data_2/everyday/0930/3.jpeg')
img4 = cv2.imread('/media/data_2/everyday/0930/4.jpeg')

img1 = cv2.resize(img1,(800,800))
img2 = cv2.resize(img2,(800,800))
img3 = cv2.resize(img3,(800,800))
img4 = cv2.resize(img4,(800,800))

# img[:, :, ::-1]是将BGR转化为RGB
plt.subplot(121) #要生成1行2列,这是第一个图plt.subplot('行','列','编号')
plt.imshow(img1[:, :, ::-1])  
plt.title('dog-1')
plt.subplot(122)
plt.imshow(img2[:, :, ::-1]) 
plt.title('dog-2')

fig = plt.figure()
plt.subplot(121)
plt.imshow(img3[:, :, ::-1])
plt.title('dog-3') 
plt.subplot(122)
plt.imshow(img4[:, :, ::-1]) 
plt.title('cat-1')
plt.show()

3. example3

import cv2
import numpy as np
import matplotlib.pyplot as plt
t=np.arange(0.0,2.0,0.1)
s=np.sin(t*np.pi)
plt.subplot(2,2,1) #要生成两行两列,这是第一个图plt.subplot('行','列','编号')
plt.plot(t,s,'b--')
plt.ylabel('y1')
plt.subplot(2,2,2) #两行两列,这是第二个图
plt.plot(2*t,s,'r--')
plt.ylabel('y2')
plt.subplot(2,2,3)#两行两列,这是第三个图
plt.plot(3*t,s,'m--')
plt.subplot(2,2,4)#两行两列,这是第四个图
plt.plot(4*t,s,'k--')
plt.show()

4. 4个数组画在一张图上

import matplotlib.pyplot as plt
train_acc_list = []
train_loss_list = []
val_acc_list = []
val_loss_list = []
##
## 数组append添加数据
##
iters = range(len(train_acc_list))
plt.figure()
# acc
plt.plot(iters, train_acc_list, 'r', label='train acc')
# loss
plt.plot(iters, train_loss_list, 'g', label='train loss')

# val_acc
plt.plot(iters, val_acc_list, 'b', label='val acc')
# val_loss
plt.plot(iters, val_loss_list, 'k', label='val loss')
plt.grid(True)
plt.xlabel('epoch')
plt.ylabel('acc-loss')
plt.legend(loc="upper right")
plt.savefig('./acc-loss.png')
plt.show()

  1. 4个数组分开画,但是在一张图上
import matplotlib.pyplot as plt
train_acc_list = []
train_loss_list = []
val_acc_list = []
val_loss_list = []
##
## 数组append添加数据
##

iters = range(len(train_acc_list))
plt.figure()
# acc
plt.subplot(2,2,1)
plt.xlabel('epoch')
plt.ylabel('train acc')
plt.legend(loc="upper right")
plt.plot(iters, train_acc_list, 'r', label='train acc')
plt.grid(True)
# loss
plt.subplot(2,2,2)
plt.xlabel('epoch')
plt.ylabel('train loss')
plt.legend(loc="upper right")
plt.plot(iters, train_loss_list, 'g', label='train loss')
plt.grid(True)

# val_acc
plt.subplot(2,2,3)
plt.xlabel('epoch')
plt.ylabel('val acc')
plt.legend(loc="upper right")
plt.plot(iters, val_acc_list, 'b', label='val acc')
plt.grid(True)
# val_loss
plt.subplot(2,2,4)
plt.xlabel('epoch')
plt.ylabel('val loss')
plt.legend(loc="upper right")
plt.plot(iters, val_loss_list, 'k', label='val loss')
plt.grid(True)
# plt.xlabel('epoch')
# plt.ylabel('acc-loss')
# plt.legend(loc="upper right")
plt.savefig('./acc-loss.png')
plt.show()

posted @ 2019-09-30 10:25  无左无右  阅读(10008)  评论(0编辑  收藏  举报