# -*- coding: utf-8 -*- """ Created on Fri Nov 3 12:43:04 2023 @author: adopi """ ############################################################################################################################ #Relevant variables to enter mapwavenumber=[460,1100] #Add wavenumber values that the maps should be created with, This must be great than just 1 wavenumber mapcolor=['Reds','Greens'] #add mapcolor corresponding to each wavenumber, this must equal the size of mapwavenumber normalize_active=0 #1 normalizes to the first value in the mapwavenumber, 0 will scale itslef to each map baseline_subtraction='yes' #'yes' means ti will baseline subtract each spectra before mapping. 'no' will not use baseline subtraction baseline_lambda=1e6 #how much the baseline is fit subtract=0 #will subtract data off the end of the wavenumbers (which starts from lowest wavenumber) transparency=1 #transparency of each map image ############################################################################################################################ import numpy as np from matplotlib import pyplot as plt import matplotlib.pyplot as plt from matplotlib.colors import LogNorm import matplotlib.pyplot as plt import matplotlib.cm as cm import matplotlib as mpl from matplotlib.ticker import AutoMinorLocator import matplotlib.lines as lines import pybaselines from matplotlib.patches import Rectangle #Prompt to open the csv datasheet from tkinter.filedialog import askopenfilename #fig = plt.figure(figsize=(3,6), dpi=150) #ax = fig.add_subplot(111) filename = askopenfilename() FILELOCATION= filename data = np.genfromtxt(FILELOCATION, delimiter='\t',skip_header=1) xlength=len(np.unique(data[:,0]-data[0,0])) ylength=len(np.unique(data[:,1]-data[0,1])) num_of_spectra=xlength*ylength NumberSpectraToPlot=num_of_spectra wavelength=int(len(data[:,2])/num_of_spectra) for u in range(0,len(mapwavenumber),1): #finds closest wavenumber value to requested and makes map based on that difflist=np.full(wavelength,0,dtype='float64') for l in range(0,wavelength,1): difflist[l]=abs(data[l,2]-mapwavenumber[u]) print(difflist[l]) for b in range(np.argmin(difflist),np.argmin(difflist)+1,1): #Need to Enter these values spectralpoint=b #needs to be in hex color format #All spectra datanum2=np.arange(NumberSpectraToPlot,dtype=int) #Specific spectra #datanum2=[30,40,49] length=len(data) data_points=length/num_of_spectra data_points=int(data_points) datanum=[l*1015 for l in datanum2] a=0 dataz=0 for i in datanum: a=a+1 baseline=[0,0] if baseline_subtraction == 'yes': baseline=pybaselines.whittaker.arpls(data[i:i+data_points-subtract,3],lam=baseline_lambda) #comment this out if you don't want baseline subtraction colorindex=int(i/data_points) # line2=lines.Line2D(data[i:i+data_points-subtract,1],data[i:i+data_points-subtract,2]-baseline[0]-offset*a,color=colorsalt[colorindex-1],linewidth=1) if i==0: dataz=data[i:i+data_points-subtract,3]-baseline[0] datax=data[i:i+data_points-subtract,0] datay=data[i:i+data_points-subtract,1] # mapZ=dataz[i+spectralpoint] if i != 0: dataz=np.append(dataz,data[i:i+data_points-subtract,3]-baseline[0]) datax=np.append(datax,data[i:i+data_points-subtract,0]) datay=np.append(datay,data[i:i+data_points-subtract,1]) # mapZ=np.append(mapZ,dataz[i+spectralpoint]) # mapZ=np.append(mapZ,dataz[i+spectralpoint]) #Make mapZ with changed data num datanum=[l*(1015-subtract) for l in datanum2] for i in datanum: # a=a+1 # baseline=[0,0] # baseline=pybaselines.whittaker.arpls(data[i:i+data_points-subtract,3],lam=1e5) #comment this out if you don't want baseline subtraction # colorindex=int(i/data_points) # line2=lines.Line2D(data[i:i+data_points-subtract,1],data[i:i+data_points-subtract,2]-baseline[0]-offset*a,color=colorsalt[colorindex-1],linewidth=1) if i==0: mapZ=dataz[i+subtract+spectralpoint] if i != 0: mapZ=np.append(mapZ,dataz[i+spectralpoint]) zlist=np.full(length,0,dtype='float64') lengthmapZ=len(mapZ) i=0 for j in range(0,lengthmapZ): for i in range(i,i+data_points): zlist[i]=mapZ[j] #list of X, Y and Z x_list = data[:,0]-data[0,0] y_list = data[:,1]-data[0,1] z_list = zlist length = np.size(z_list) #list of X and Y values (np.unique removes redundancies) N_x = np.unique(x_list) N_y = np.unique(y_list) X, Y = np.meshgrid(N_x,N_y) length_x = np.size(N_x) length_y = np.size(N_y) #define empty intensity matrix Z = np.full((length_x, length_y), 0) #the f function will chase the Z values corresponding # to a given x and y value def f(x, y): for i in range(0, length): if (x_list[i] == x) and (y_list[i] == y): return z_list[i] #a loop will now populate the Z matrix for i in range(0, length_x ): for j in range(0, length_y ): Z[i,j] = f(N_x[i], N_y[j]) print(i) print(j) #and then comes the plot, with the colour-blind-friendly viridis colourmap fig = plt.figure(figsize=(4,3), dpi=150) ax=fig.add_subplot() # plt.pcolormesh(X,Y, np.transpose(Z),shading='gouraud',cmap='viridis',edgecolors='none',vmin=0,vmax=3000) plt.imshow(np.transpose(np.rot90(Z,k=1,axes=(0,1))),cmap=mapcolor[u],vmin=0,vmax=max(mapZ)*1.2,interpolation='spline16',extent=[0,max(N_x),0,max(N_y)],alpha=transparency) #plt.contourf(Y, X, np.transpose(Z), 20, origin = 'lower', cmap=cm.viridis, alpha = 1.0,antialiased=False); cbar = plt.colorbar() cbar.set_label('Intensity (a.u.)') #optional countour lines: """contours = plt.contour(X, Y, np.transpose(Z), colors='black'); plt.clabel(contours, inline=True, fontsize=8) """ plt.xlabel('Distance (µm)') label=str(int(data[spectralpoint,2])) labelpoint=str(spectralpoint) plt.title( label+ ' $\mathregular{cm^{-1}}$') # plt.axis('square') # # plt.tick_params(direction='out', length=6, width=1.75, colors='black',which='major',left='off',labelsize=8) # plt.tick_params(direction='out', length=3.5, width=1.75, colors='black',which='minor',left='off',labelsize=8) # ax.xaxis.set_minor_locator(AutoMinorLocator(2)) # ax.yaxis.set_minor_locator(AutoMinorLocator(1)) plt.tight_layout() plt.show()