matplotlib绘制电子鼻传感响应曲线
记录一下最近利用matplotlib绘制电子鼻响应曲线(老师的项目工作,会省掉一些东西)
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import os
mpl.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
data = pd.read_csv('') ##读取要绘制的数据,一般是txt
data = data.iloc[:,5:29]
data.columns = [i for i in range(24)]
name = ['温度1','温度2','湿度1','湿度2','压力1','压力2'] ## 传感器名称,省略了一些
y_ticks = np.arange(-1000,4200,500)
x_ticks = np.arange(0,820,50)
for i in range(24):
plt.plot(data.iloc[:,i])
plt.xlim(0,820)
plt.ylim(-1000,4200)
plt.xticks(x_ticks,size=20)
plt.yticks(y_ticks,size=20)
t1 = 'P13'
t2 = 'P14'
t3 = 'P15'
t4 = 'P17'
plt.text(50, -300, t1, ha='left', rotation=0, wrap=True, bbox=dict(boxstyle='square,pad=0.5', fc='white', ec='k',lw=1 ,alpha=0.5),size=15)
plt.text(120, -300, t2, ha='left', rotation=0, wrap=True, bbox=dict(boxstyle='square,pad=0.5', fc='white', ec='k',lw=1 ,alpha=0.5),size=15)
plt.text(325, -300, t3, ha='left', rotation=0, wrap=True, bbox=dict(boxstyle='square,pad=0.5', fc='white', ec='k',lw=1 ,alpha=0.5),size=15)
plt.text(675, -300, t4, ha='left', rotation=0, wrap=True, bbox=dict(boxstyle='square,pad=0.5', fc='white', ec='k',lw=1 ,alpha=0.5),size=15)
t5 = ' P13:空气基线\nP14:空气后基线\nP15:进样阶段\nP17:清洗阶段'
plt.text(830, -500, t5, ha='left', rotation=0, wrap=True, bbox=dict(boxstyle='square,pad=0.5', fc='white', ec='k',lw=1 ,alpha=0.5),size=15)
plt.legend(name,bbox_to_anchor=(1,0.626), loc='center left',fontsize=12)
plt.axvline(x=100,ls=':',c='black') ## 绘制垂直直线
plt.axvline(x=160,ls=':',c='green')
plt.axvline(x=520,ls=':',c='red')
plt.xlabel('数据采样点',size=20)
plt.ylabel('传感器数值',size=20)
plt.show()
上图是绘图结果。若需要循环绘制图片,并保存可以在保存数据的时候可以使用这样的语句这样可以使保存的图像清晰:
fig = plt.gcf()
fig.set_size_inches((20, 11), forward=False)
fig.savefig(save_path+pathdir+'.png')
plt.clf() # 循环绘制,在保存好后会清除已经绘制好的内容以便下一次绘制。