1.将不同的图绘制在一起
import cv2
import numpy as np
def mul_pics(img1,img2,img3,img4,img5,img6,img7,img8):
"""为了结合多张图绘制成一张图 利用 np.hstack、np.vstack实现一幅图像中显示多幅图片"""
results = (img1,img2,img3,img4)
results1 = (img5,img6,img7,img8)
# np.hstack()将两个数组按列放到一起
line1 = np.hstack(results[:2])
line2 = np.hstack(results[2:4])
line3 = np.hstack(results1[:2])
line4 = np.hstack(results1[2:4])
combined = np.vstack([line1, line2]) # 将多个数组按行放到一起
combined1 = np.vstack([line3, line4]) # 将多个数组按行放到一起
cv2.namedWindow('Result', cv2.WINDOW_NORMAL)
cv2.namedWindow('Result1', cv2.WINDOW_NORMAL)
cv2.imshow('Result', combined)
cv2.imshow('Result1', combined1)
cv2.imwrite('F:\epycharm\HSB_match\pic\Result_a1_a4.jpg',combined)
cv2.imwrite('F:\epycharm\HSB_match\pic\Result_a4_a8.jpg',combined1)
cv2.waitKey(0)
# img1 = cv2.imread('F:\epycharm\HSB_match\pic\A1.png',cv2.IMREAD_COLOR)
# img2 = cv2.imread('F:\epycharm\HSB_match\pic\A2.png',cv2.IMREAD_COLOR)
# img3 = cv2.imread('F:\epycharm\HSB_match\pic\A3.png',cv2.IMREAD_COLOR)
# img4 = cv2.imread('F:\epycharm\HSB_match\pic\A4.png',cv2.IMREAD_COLOR)
# img5 = cv2.imread('F:\epycharm\HSB_match\pic\A5.png',cv2.IMREAD_COLOR)
# img6 = cv2.imread('F:\epycharm\HSB_match\pic\A6.png',cv2.IMREAD_COLOR)
# img7 = cv2.imread('F:\epycharm\HSB_match\pic\A7.png',cv2.IMREAD_COLOR)
# img8 = cv2.imread('F:\epycharm\HSB_match\pic\A8.png',cv2.IMREAD_COLOR)
# mul_pics(img1,img2,img3,img4,img5,img6,img7,img8)
2.将不同txt文档中的数据绘制多条折线图
import matplotlib.pyplot as plt
file = open('F:\epycharm\HSB_match\excel_file\决策树.txt') #决策
file1 = open('F:\epycharm\HSB_match\excel_file\随机森林20.txt') #随机-20
file2 = open('F:\epycharm\HSB_match\excel_file\随机森林25.txt') #随机-25
file3 = open('F:\epycharm\HSB_match\excel_file\随机森林30.txt') #随机-30
data = file.readlines() #读取文档数据
data1 = file1.readlines() #读取文档数据
data2 = file2.readlines() #读取文档数据
data3 = file3.readlines() #读取文档数据
para_1 = [] #新建列表,决策
para_2 = [] #新建列表,随机-25
para_3 = [] #新建列表,随机-20
para_4 = [] #新建列表,随机-30
for num in data:
# split用于将每一行数据用逗号分割成多个对象
#取分割后的第0列,转换成float格式后添加到para_1列表中
para_1.append(float(num.split(',')[0]))
for num in data1:
# split用于将每一行数据用逗号分割成多个对象
# 取分割后的第0列,转换成float格式后添加到para_1列表中
para_2.append(float(num.split(',')[0]))
for num in data2:
# split用于将每一行数据用逗号分割成多个对象
#取分割后的第0列,转换成float格式后添加到para_1列表中
para_3.append(float(num.split(',')[0]))
for num in data3:
# split用于将每一行数据用逗号分割成多个对象
#取分割后的第0列,转换成float格式后添加到para_1列表中
para_4.append(float(num.split(',')[0]))
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
para_1.sort()
# for i in range(len(para_1)):
# f = open('F:\epycharm\HSB_match\excel_file\data2.txt', 'a')
# f.write(str(i))
# f.write('\n')
#
para_2.sort()
para_3.sort()
print(min(para_1))
print(max(para_1))
print('............')
print(min(para_2))
print(max(para_2))
print('............')
print(min(para_3))
print(max(para_3))
print('............')
para_4.sort()
# for i in range(len(para_2)):
# f = open('F:\epycharm\HSB_match\excel_file\data3.txt', 'a')
# f.write(str(i))
# f.write('\n')
plt.figure()
plt.xlabel('steps', fontsize=10) # x轴表示
plt.ylabel('R2_score', fontsize=10) # y轴表示
plt.title('决策树R2分数对比图')
plt.plot(para_1,markerfacecolor='r',label='决策树')
plt.plot(para_2,markerfacecolor='b',label='随机森林-20')
plt.plot(para_3,markerfacecolor='b',label='随机森林-25')
# plt.plot(para_4,markerfacecolor='b',label='随机森林-30')
plt.legend()
plt.show()
如果对于一行有两个数据
def loadData0(flieName1, flieName2):
inFile1 = open(flieName1, 'r') # 以只读方式打开某fileName文件
inFile2 = open(flieName2, 'r')
# 定义两个空list,用来存放文件中的数据
x = []
y = []
for index, line in enumerate(inFile1):
x.append(float(line))
for index, line in enumerate(inFile2):
y.append(float(line))
return x, y # 注意,若返回的为元组(x, y),则画图的时候python不能区分大小