功能
- 数据:读取表格。可指定Sheet表……也可指定Sheet表中的特定列
- 制作多个子图,显示在一张图
- 设置各子图的图标题、轴标题等
代码
import matplotlib.pyplot as plt
import xlrd
#设置字体格式,以显示中文
plt.rcParams['font.sans-serif'] = ['SimHei']
x_data = []
y1_data = []
y2_data = []
y3_data = []
#读取表格数据
data = xlrd.open_workbook(r'…….xlsx')
table = data.sheets()[1] # 表格中的Sheet2
x_data = list(range(100)) # x值为1~100
cap1 = table.col_values(0)#读取Sheet2的第1列
cap2 = table.col_values(1)#读取Sheet2的第2列
cap3 = table.col_values(2)#读取Sheet2的第3列
#print(cap) #查看是否正确读取
for i in range(1, 101):
y1_data.append(cap1[i])
for i in range(1, 101):
y2_data.append(cap2[i])
for i in range(1, 101):
y3_data.append(cap3[i])
#制作各个子图,并设置相关参数
fig = plt.figure()
ax1 = fig.add_subplot(2,3,1)
ax2 = fig.add_subplot(2,3,2)
ax3 = fig.add_subplot(2,3,3)
ax1.plot(x_data,y1_data,c='red')
ax1.set_title('F1值随迭代次数的变化')
ax1.set_xlabel('迭代次数')
ax1.set_ylabel('F1值')
ax1.set_ylim(ymin = 0.5, ymax=1.0)
ax2.plot(x_data,y2_data,c='blue')
ax2.set_title('Precision值随迭代次数的变化')
ax2.set_xlabel('迭代次数')
ax2.set_ylabel('Precision值')
ax2.set_ylim(ymin = 0.5, ymax=1.0)
ax3.plot(x_data,y3_data,c='green')
ax3.set_title('Recall值随迭代次数的变化')
ax3.set_xlabel('迭代次数')
ax3.set_ylabel('Recall值')
ax3.set_ylim(ymin = 0.5, ymax=1.0)
plt.show()