Skip to content
This repository was archived by the owner on Apr 1, 2026. It is now read-only.

Commit 6d123b5

Browse files
modify plot export settings
1 parent ae5c073 commit 6d123b5

1 file changed

Lines changed: 133 additions & 129 deletions

File tree

PythonGUI_apps/UV_Vis_analysis/uv_vis_analysis.py

Lines changed: 133 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -30,135 +30,139 @@
3030
plt.rc('axes', linewidth = 3.5)
3131

3232
class MainWindow(TemplateBaseClass):
33-
34-
def __init__(self):
35-
super(TemplateBaseClass, self).__init__()
36-
37-
# Create the main window
38-
self.ui = WindowTemplate()
39-
self.ui.setupUi(self)
40-
41-
#setup uv vis plot
42-
self.absorbance_plot_layout = pg.GraphicsLayoutWidget()
43-
self.ui.absorbance_plot_container.layout().addWidget(self.absorbance_plot_layout)
44-
self.absorbance_plot = self.absorbance_plot_layout.addPlot(title="Wavelengths vs. Absorbance")
45-
self.absorbance_plot.setLabel('bottom', 'Wavelength', unit='nm')
46-
self.absorbance_plot.setLabel('left', 'Absorbance', unit='a.u.')
47-
48-
self.correction_region = pg.LinearRegionItem()
49-
#self.correction_region.setZValue(10)
50-
self.correction_region_min = 600
51-
self.correction_region_max = 900
52-
self.correction_region.setRegion((self.correction_region_min, self.correction_region_max))
53-
54-
self.ui.actionLoad_data.triggered.connect(self.open_data_file)
55-
self.ui.plot_absorbance_pushButton.clicked.connect(self.plot_absorbance)
56-
self.ui.clear_uvvis_pushButton.clicked.connect(self.clear_uvvis)
57-
self.ui.export_uv_vis_pushButton.clicked.connect(self.export_uv_vis)
58-
self.correction_region.sigRegionChanged.connect(self.update_correction_region)
59-
60-
#setup tauc plot
61-
self.tauc_plot_layout = pg.GraphicsLayoutWidget()
62-
self.ui.tauc_plot_container.layout().addWidget(self.tauc_plot_layout)
63-
self.tauc_plot = self.tauc_plot_layout.addPlot(title="Tauc plot fit")
64-
self.tauc_plot.setLabel('bottom', 'hv', unit='ev')
65-
y_label = '(ahv)' + chr(0x00B2) #char is superscripted 2
66-
self.tauc_plot.setLabel('left', y_label)
67-
68-
self.ui.plot_tauc_pushButton.clicked.connect(self.plot_tauc)
69-
self.ui.clear_tauc_pushButton.clicked.connect(self.clear_tauc)
70-
self.ui.export_tauc_pushButton.clicked.connect(self.export_tauc)
71-
72-
self.show()
73-
74-
"""Open Scan Files"""
75-
def open_data_file(self):
76-
try:
77-
self.filename = QtWidgets.QFileDialog.getOpenFileName(self)
78-
self.data = np.loadtxt(self.filename[0], delimiter = ',', skiprows = 1)
79-
self.Wavelength = self.data[:,0] # in nm
80-
self.Absorbance = self.data[:,1]
81-
except Exception as err:
82-
print(format(err))
83-
84-
def update_correction_region(self):
85-
self.correction_region_min, self.correction_region_max = self.correction_region.getRegion()
86-
87-
def plot_absorbance(self):
88-
try:
89-
self.scatter_corrected = False
90-
self.plotted_absorbance = self.Absorbance
91-
if self.ui.correct_for_scattering_checkBox.isChecked():
92-
self.scatter_corrected = True
93-
self.plotted_absorbance = self.Absorbance - np.mean(self.Absorbance[(self.Wavelength>self.correction_region_min) & (self.Wavelength<self.correction_region_max)])
94-
self.absorbance_plot.plot(self.Wavelength, self.plotted_absorbance, pen='r', clear=True)
95-
self.absorbance_plot.addItem(self.correction_region, ignoreBounds=True)
96-
except Exception as err:
97-
print(format(err))
98-
99-
def clear_uvvis(self):
100-
self.absorbance_plot.clear()
101-
102-
def plot_tauc(self):
103-
try:
104-
self.hv_min = self.ui.hv_min_spinBox.value()
105-
self.hv_max = self.ui.hv_max_spinBox.value()
106-
self.hv = 1240/self.Wavelength
107-
self.Alpha_hv = (self.Absorbance * self.hv)**2.0
108-
self.index = (self.hv > self.hv_min) & (self.hv < self.hv_max)
109-
model = np.polyfit(self.hv[self.index], self.Alpha_hv[self.index], 1)
110-
self.Alpha_hv_fit = self.hv * model[0] + model[1] #This is the linear fit
111-
self.tauc_plot.plot(self.hv, self.Alpha_hv, pen='r')
112-
self.tauc_plot.plot(self.hv, self.Alpha_hv_fit, pen='k')
113-
114-
self.Eg = - model[1]/model[0]
115-
self.ui.bandgap_label.setText(str(self.Eg))
116-
except:
117-
pass
118-
119-
def clear_tauc(self):
120-
self.tauc_plot.clear()
121-
122-
def export_uv_vis(self):
123-
try:
124-
filename = QtWidgets.QFileDialog.getSaveFileName(self,caption="Filename with EXTENSION")
125-
plt.figure(figsize=(8,6))
126-
plt.tick_params(direction='out', length=8, width=3.5)
127-
plt.plot(self.Wavelength, self.plotted_absorbance, linewidth = 3, color = 'r')
128-
if self.scatter_corrected:
129-
plt.xlim(self.correction_region_min, self.correction_region_max)
130-
else:
131-
plt.xlim(np.min(self.Wavelength), np.max(self.Wavelength))
132-
plt.ylim(np.min(self.plotted_absorbance), np.max(self.plotted_absorbance))
133-
plt.xlabel('Wavelength (nm)', fontsize = 20)
134-
plt.ylabel('Absorbance (a.u.)', fontsize = 20)
135-
plt.savefig(filename[0],bbox_inches='tight', dpi=300)
136-
plt.close()
137-
except:
138-
pass
139-
140-
def export_tauc(self):
141-
try:
142-
filename = QtWidgets.QFileDialog.getSaveFileName(self,caption="Filename with EXTENSION")
143-
plt.figure(figsize=(8,6))
144-
plt.tick_params(direction='out', length=8, width=3.5)
145-
plt.plot(self.hv, self.Alpha_hv, linewidth = 3, color = 'r')
146-
plt.plot(self.hv, self.Alpha_hv_fit, linewidth = 2, color = 'black')
147-
plt.xlim(1,2)
148-
plt.ylim(0, np.max(self.Alpha_hv[self.index]) + 1)
149-
plt.xlabel('h$\\nu$ (eV)', fontsize = 20)
150-
plt.ylabel('($\\alpha$h$\\nu$)$^2$', fontsize = 20)
151-
#plt.title(Plot_title, fontsize = 20)
152-
153-
plt.text(1.2, 1.2, r'E$_{g}$ = %.2f eV'%self.Eg, fontsize = 15)
154-
plt.tight_layout()
155-
plt.savefig(filename[0],bbox_inches='tight', dpi=300)
156-
plt.close()
157-
except:
158-
pass
33+
34+
def __init__(self):
35+
super(TemplateBaseClass, self).__init__()
36+
37+
# Create the main window
38+
self.ui = WindowTemplate()
39+
self.ui.setupUi(self)
40+
41+
#setup uv vis plot
42+
self.absorbance_plot_layout = pg.GraphicsLayoutWidget()
43+
self.ui.absorbance_plot_container.layout().addWidget(self.absorbance_plot_layout)
44+
self.absorbance_plot = self.absorbance_plot_layout.addPlot(title="Wavelengths vs. Absorbance")
45+
self.absorbance_plot.setLabel('bottom', 'Wavelength', unit='nm')
46+
self.absorbance_plot.setLabel('left', 'Absorbance', unit='a.u.')
47+
48+
self.correction_region = pg.LinearRegionItem()
49+
#self.correction_region.setZValue(10)
50+
self.correction_region_min = 600
51+
self.correction_region_max = 900
52+
self.correction_region.setRegion((self.correction_region_min, self.correction_region_max))
53+
54+
self.ui.actionLoad_data.triggered.connect(self.open_data_file)
55+
self.ui.plot_absorbance_pushButton.clicked.connect(self.plot_absorbance)
56+
self.ui.clear_uvvis_pushButton.clicked.connect(self.clear_uvvis)
57+
self.ui.export_uv_vis_pushButton.clicked.connect(self.export_uv_vis)
58+
self.correction_region.sigRegionChanged.connect(self.update_correction_region)
59+
60+
#setup tauc plot
61+
self.tauc_plot_layout = pg.GraphicsLayoutWidget()
62+
self.ui.tauc_plot_container.layout().addWidget(self.tauc_plot_layout)
63+
self.tauc_plot = self.tauc_plot_layout.addPlot(title="Tauc plot fit")
64+
self.tauc_plot.setLabel('bottom', 'hv', unit='ev')
65+
y_label = '(ahv)' + chr(0x00B2) #char is superscripted 2
66+
self.tauc_plot.setLabel('left', y_label)
67+
68+
self.ui.plot_tauc_pushButton.clicked.connect(self.plot_tauc)
69+
self.ui.clear_tauc_pushButton.clicked.connect(self.clear_tauc)
70+
self.ui.export_tauc_pushButton.clicked.connect(self.export_tauc)
71+
72+
self.show()
73+
74+
"""Open Scan Files"""
75+
def open_data_file(self):
76+
try:
77+
self.filename = QtWidgets.QFileDialog.getOpenFileName(self)
78+
self.data = np.loadtxt(self.filename[0], delimiter = ',', skiprows = 1)
79+
self.Wavelength = self.data[:,0] # in nm
80+
self.Absorbance = self.data[:,1]
81+
except Exception as err:
82+
print(format(err))
83+
84+
def update_correction_region(self):
85+
self.correction_region_min, self.correction_region_max = self.correction_region.getRegion()
86+
87+
def plot_absorbance(self):
88+
try:
89+
self.scatter_corrected = False
90+
self.plotted_absorbance = self.Absorbance
91+
if self.ui.correct_for_scattering_checkBox.isChecked():
92+
self.scatter_corrected = True
93+
self.plotted_absorbance = self.Absorbance - np.mean(self.Absorbance[(self.Wavelength>self.correction_region_min) & (self.Wavelength<self.correction_region_max)])
94+
self.absorbance_plot.plot(self.Wavelength, self.plotted_absorbance, pen='r', clear=True)
95+
self.absorbance_plot.setYRange(0, 4)
96+
self.absorbance_plot.addItem(self.correction_region, ignoreBounds=True)
97+
except Exception as err:
98+
print(format(err))
99+
100+
def clear_uvvis(self):
101+
self.absorbance_plot.clear()
102+
103+
def plot_tauc(self):
104+
try:
105+
self.hv_min = self.ui.hv_min_spinBox.value()
106+
self.hv_max = self.ui.hv_max_spinBox.value()
107+
self.hv = 1240/self.Wavelength
108+
self.Alpha_hv = (self.Absorbance * self.hv)**2.0
109+
self.index = (self.hv > self.hv_min) & (self.hv < self.hv_max)
110+
model = np.polyfit(self.hv[self.index], self.Alpha_hv[self.index], 1)
111+
self.Alpha_hv_fit = self.hv * model[0] + model[1] #This is the linear fit
112+
self.tauc_plot.plot(self.hv, self.Alpha_hv, pen='r')
113+
self.tauc_plot.plot(self.hv, self.Alpha_hv_fit, pen='k')
114+
self.tauc_plot.setXRange(1,2)
115+
self.tauc_plot.setYRange(0, np.max(self.Alpha_hv[self.index]) + 1)
116+
117+
self.Eg = - model[1]/model[0]
118+
self.ui.bandgap_label.setText(str(self.Eg))
119+
except:
120+
pass
121+
122+
def clear_tauc(self):
123+
self.tauc_plot.clear()
124+
125+
def export_uv_vis(self):
126+
try:
127+
filename = QtWidgets.QFileDialog.getSaveFileName(self,caption="Filename with EXTENSION")
128+
plt.figure(figsize=(8,6))
129+
plt.tick_params(direction='out', length=8, width=3.5)
130+
plt.plot(self.Wavelength, self.plotted_absorbance, linewidth = 3, color = 'r')
131+
if self.scatter_corrected:
132+
plt.xlim(self.correction_region_min, self.correction_region_max)
133+
plt.ylim(0, np.max(self.plotted_absorbance[(self.Wavelength>self.correction_region_min)]) +0.5)
134+
else:
135+
plt.xlim(self.correction_region_min, self.correction_region_max)
136+
plt.ylim(0, np.max(self.plotted_absorbance[(self.Wavelength>self.correction_region_min)] +0.5))
137+
plt.xlabel('Wavelength (nm)', fontsize = 20)
138+
plt.ylabel('Absorbance (a.u.)', fontsize = 20)
139+
plt.savefig(filename[0],bbox_inches='tight', dpi=300)
140+
plt.close()
141+
except:
142+
pass
143+
144+
def export_tauc(self):
145+
try:
146+
filename = QtWidgets.QFileDialog.getSaveFileName(self,caption="Filename with EXTENSION")
147+
plt.figure(figsize=(8,6))
148+
plt.tick_params(direction='out', length=8, width=3.5)
149+
plt.plot(self.hv, self.Alpha_hv, linewidth = 3, color = 'r')
150+
plt.plot(self.hv, self.Alpha_hv_fit, linewidth = 2, color = 'black')
151+
plt.xlim(1,2)
152+
plt.ylim(0, np.max(self.Alpha_hv[self.index]) + 1)
153+
plt.xlabel('h$\\nu$ (eV)', fontsize = 20)
154+
plt.ylabel('($\\alpha$h$\\nu$)$^2$', fontsize = 20)
155+
#plt.title(Plot_title, fontsize = 20)
156+
157+
plt.text(1.2, 1.2, r'E$_{g}$ = %.2f eV'%self.Eg, fontsize = 15)
158+
plt.tight_layout()
159+
plt.savefig(filename[0],bbox_inches='tight', dpi=300)
160+
plt.close()
161+
except:
162+
pass
159163

160164
"""Run the Main Window"""
161165
def run():
162-
win = MainWindow()
163-
QtGui.QApplication.instance().exec_()
164-
return win
166+
win = MainWindow()
167+
QtGui.QApplication.instance().exec_()
168+
return win

0 commit comments

Comments
 (0)