从xlxs表格中读取数据画图,本例子包含各种作折线图的细节。若有问题可留言。
表格中数据为三列n行如下图所示。
1.单坐标轴例子
import xlrd import matplotlib.pyplot as plt from matplotlib.pyplot import plot,savefig from pylab import * config = { "font.family": 'serif', "font.size": 15, "mathtext.fontset": 'stix', "font.serif": ['SimSun'], } rcParams.update(config) file_name = xlrd.open_workbook('C://Users//自定义路径//表格名称.xlsx')#得到文件 table =file_name.sheets()[0]#得到sheet nrows = table.nrows #总行数 ncols = table.ncols #总列数 i = 0 time = [] y1 = [] y2 = [] while i < nrows: ctype_temp = table.cell(i, 1).ctype #得到数字列数据的格式 time_temp = table.row_values(i)[0] #得到数字列数据 y1_temp = table.row_values(i)[1] y2_temp = table.row_values(i)[2] if(ctype_temp == 2): time.append(time_temp) y1.append(y1_temp) y2.append(y2_temp) i=i+1 plt.figure(figsize=(20,10)) plt.xlim(0, 69) plt.ylim(0.5,3.5) x_major_locator=MultipleLocator(2) plt.ylabel(r'标题',labelpad=-15,size=20,position=(-10,1.02),rotation=0) plt.xticks(rotation=50) plt.xlabel(r'标题',labelpad=-37,size=20,position=(1.02,-20),rotation=0) plt.xticks(fontfamily="Times New Roman") plt.yticks(fontfamily="Times New Roman") ax=plt.gca() ax.xaxis.set_major_locator(x_major_locator) ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') plt.tick_params(labelsize=20) plt.plot(time,y1,label=r'标题',linewidth=3,color = 'blue') plt.plot(time,y2,label=r'$\mathrm{English}$标题',linewidth=3,linestyle = '--',color = 'orange') plt.legend(loc=2,prop={'size':20}) savefig('C://Users//自定义路径//'+'图片名称.png',dpi = 500)
生成图片如下所示。
2.双坐标轴例子
file_name = xlrd.open_workbook('C://自定义路径//表格名称.xlsx')#得到文件 table =file_name.sheets()[0]#得到sheet nrows = table.nrows #总行数 ncols = table.ncols #总列数 i = 0 time = [] y1 = [] y2 = [] while i < nrows: ctype_temp = table.cell(i, 1).ctype #得到数字列数据的格式 time_temp = table.row_values(i)[0] #得到数字列数据 y1_temp = table.row_values(i)[1] y2_temp = table.row_values(i)[2] if(ctype_temp == 2): time.append(time_temp) y1.append(y1_temp) y2.append(y2_temp) i=i+1 plt.figure(figsize=(20,10)) plt.xlim(0, 69) plt.ylim(0,1500) x_major_locator=MultipleLocator(2) plt.ylabel(r'标题',labelpad=-23,size=20,position=(-6,1.01),rotation=0) plt.xlabel(r'标题',labelpad=-37,size=20,position=(1.02,-20),rotation=0) plt.xticks(rotation=50) plt.xticks(fontfamily="Times New Roman") plt.yticks(fontfamily="Times New Roman") ax=plt.gca() ax.xaxis.set_major_locator(x_major_locator) ax.spines['top'].set_color('none') plt.tick_params(labelsize=20) ax=plt.gca() ax.xaxis.set_major_locator(x_major_locator) ax.spines['top'].set_color('none') plt.plot(time,y1,label=r'$\mathrm{English}$标题',linewidth=3,color = 'blue') l1 = plt.legend(loc=2,prop={'size':20}) ax2=ax.twinx() ax2.plot(time,y2,label=r'$\mathrm{English}$标题',linewidth=3,linestyle = '--',color = 'orange') ax2.set_ylabel(r'%',labelpad=-13,size=20,position=(0,1.047),rotation=0) ax2.set_ylim([0.5,3]) ax2.tick_params(labelsize=20) plt.yticks(fontfamily="Times New Roman") ax=plt.gca() ax.xaxis.set_major_locator(x_major_locator) ax.spines['top'].set_color('none') plt.legend(loc=1,prop={'size':20}) savefig('C://自定义路径//'+'图片名称.png',dpi = 500)