add chart

This commit is contained in:
nazarchuk.as 2022-11-08 19:55:59 +03:00
parent 97cc7cac6b
commit d879a6a6da
4 changed files with 67 additions and 34 deletions

View File

@ -5,6 +5,7 @@ import numpy as np
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
from scipy.optimize import curve_fit from scipy.optimize import curve_fit
import time import time
import pandas as pd
from PyQt6 import QtCore from PyQt6 import QtCore
from PyQt6.QtGui import QAction, QIcon, QPixmap from PyQt6.QtGui import QAction, QIcon, QPixmap
from PyQt6.QtWidgets import (QHBoxLayout, from PyQt6.QtWidgets import (QHBoxLayout,
@ -176,11 +177,11 @@ class MainExperimentDataWindow(AbstractWindow):
self.resize(1400, 800) self.resize(1400, 800)
# make masthead # make masthead
self.dataname = 'data.csv' self.parent.dataname = 'data.csv'
head_1 = 'I_0,mA' head_1 = 'I_0,mA'
head_2 = 'U_34,mV' head_2 = 'U_34,mV'
head_3 = 't,s' head_3 = 't,s'
with open(os.path.join(self.parent.folder, self.dataname), 'w') as file: with open(os.path.join(self.parent.folder, self.parent.dataname), 'w') as file:
wr = csv.writer(file) wr = csv.writer(file)
wr.writerow([head_1, head_2, head_3]) wr.writerow([head_1, head_2, head_3])
@ -246,7 +247,7 @@ class MainExperimentDataWindow(AbstractWindow):
v = str((current_time-self.start_time)/60) v = str((current_time-self.start_time)/60)
a = v a = v
t = str(current_time-self.start_time) t = str(current_time-self.start_time)
with open(os.path.join(self.parent.folder, self.dataname), 'a') as file: with open(os.path.join(self.parent.folder, self.parent.dataname), 'a') as file:
wr = csv.writer(file) wr = csv.writer(file)
wr.writerow([v, a, str(current_time-self.start_time)]) wr.writerow([v, a, str(current_time-self.start_time)])
self.table.insertRow(self.table.rowCount()) self.table.insertRow(self.table.rowCount())
@ -281,7 +282,7 @@ class MainExperimentDataWindow(AbstractWindow):
f_volt.close() f_volt.close()
f_amp.close() f_amp.close()
current_time = round(time.time()*1000) current_time = round(time.time()*1000)
with open(os.path.join(self.parent.folder, self.dataname), 'a') as file: with open(os.path.join(self.parent.folder, self.parent.dataname), 'a') as file:
wr = csv.writer(file) wr = csv.writer(file)
wr.writerow([v, a, str(current_time-self.start_time)]) wr.writerow([v, a, str(current_time-self.start_time)])
@ -290,14 +291,22 @@ class MainExperimentChartWindow(AbstractWindow):
def __init__(self, parent): def __init__(self, parent):
super().__init__() super().__init__()
self.setWindowTitle('Основной эксперимент. Обработка данных')
self.parent = parent self.parent = parent
self.parent.chartname = 'Chart'
self.parent.data = Data(data_filename=os.path.join(self.parent.folder, self.parent.dataname),
saving=os.path.join(self.parent.folder, self.parent.chartname))
self.parent.data.read_csv()
self.parent.data.x=self.parent.data.data['I_0,mA']
self.parent.data.y=self.parent.data.data['U_34,mV']
self.parent.data.make_grafic()
self.setWindowTitle('Основной эксперимент. Обработка данных')
self.resize(1400, 800) self.resize(1400, 800)
self.centralwidget = QWidget() self.centralwidget = QWidget()
self.setCentralWidget(self.centralwidget) self.setCentralWidget(self.centralwidget)
pixmap = QPixmap('arrow.png') pixmap = QPixmap(os.path.join(self.parent.folder, self.parent.chartname))
self.label = QLabel(self) self.label = QLabel(self)
self.label.setPixmap(pixmap) self.label.setPixmap(pixmap)
self.label.resize(pixmap.width(), pixmap.height()) self.label.resize(pixmap.width(), pixmap.height())
@ -310,25 +319,23 @@ class MainExperimentChartWindow(AbstractWindow):
self.hbox_layout.addWidget(self.text, 0, 1) self.hbox_layout.addWidget(self.text, 0, 1)
class Data: class Data:
def __init__(self, x, y, xlabel, ylabel, caption, xerr, yerr, through_0, def __init__(self, x=[], y=[], xlabel='', ylabel='', caption='', xerr=None,
data_filename, color=None, centering=None, size=15, yerr=None, through_0=0, data_filename='', color=None,
coefficient=[0.9, 1.1]): centering=None, size=15, coefficient=[0.9, 1.1], saving=None):
self.x = x self.x = x
self.y = y self.y = y
self.xlabel = xlabel self.xlabel = xlabel
self.ylabel = ylabel self.ylabel = ylabel
self.caption = caption self.caption = caption
if type(yerr) == float or type(yerr) == int:
self.yerr = [yerr for _ in self.y]
else:
self.yerr = yerr
if type(xerr) == float or type(xerr) == int:
self.xerr = [xerr for _ in self.x]
else:
self.xerr=xerr self.xerr=xerr
self.yerr=yerr
self.make_errors(xerr, yerr)
self.through_0 = through_0 self.through_0 = through_0
if not self.color: if not color:
self.color = ['limegreen', 'indigo'] self.color = ['limegreen', 'indigo']
else: else:
self.color = color self.color = color
@ -336,13 +343,33 @@ class Data:
self.size = size self.size = size
self.coefficient = coefficient self.coefficient = coefficient
self.data_filename=data_filename self.data_filename=data_filename
self.saving=saving
def make_errors(self, xerr=None, yerr=None):
if not xerr: xerr=self.xerr
if not yerr: yerr=self.yerr
if not xerr: xerr=0
if not yerr: yerr=0
if not yerr or type(yerr) == float or type(yerr) == int:
self.yerr = [yerr for _ in self.y]
else:
self.yerr = yerr
if not xerr or type(xerr) == float or type(xerr) == int:
self.xerr = [xerr for _ in self.x]
else:
self.xerr = xerr
def read_csv(self): def read_csv(self):
with open(self.data_filename) as file: with open(self.data_filename) as file:
reader = list(csv.reader(file, delimiter=';', reader = list(csv.reader(file))
quotechar=',', quoting=csv.QUOTE_MINIMAL))
data = np.array(reader) data = np.array(reader)
data = np.transpose(data) dic=[]
for i in range(len(data[0])):
micro_data = []
for j in range(len(data)):
micro_data.append(data[j][i])
dic.append(micro_data)
data=dic
dic = {} dic = {}
for i in range(len(data)): for i in range(len(data)):
dic[data[i][0]] = np.array(data[i][1:]).astype(np.float) dic[data[i][0]] = np.array(data[i][1:]).astype(np.float)
@ -350,15 +377,15 @@ class Data:
self.data=data self.data=data
def make_point_grafic(self): def make_point_grafic(self):
self.make_errors()
if self.xerr[1] != 0 or self.yerr[1] != 0: if self.xerr[1] != 0 or self.yerr[1] != 0:
plt.errorbar(self.x, self.y, yerr=self.yerr, xerr=self.xerr, linewidth=4, plt.errorbar(self.x, self.y, yerr=self.yerr, xerr=self.xerr, linewidth=4,
linestyle='', label=self.caption, color=self.color, linestyle='', label=self.caption, color=self.color[0],
ecolor=self.color, elinewidth=1, capsize=3.4, ecolor=self.color[0], elinewidth=1, capsize=3.4,
capthick=1.4) capthick=1.4)
else: else:
plt.scatter(self.x, self.y, linewidth=0.005, label=self.caption, plt.scatter(self.x, self.y, linewidth=0.005, label=self.caption,
color=self.color, edgecolor='black', s=self.size) color=self.color[0], edgecolor='black', s=self.size)
if not self.centering: if not self.centering:
plt.xlabel(self.xlabel) plt.xlabel(self.xlabel)
@ -373,7 +400,7 @@ class Data:
self.ax.set_ylabel(self.xlabel, labelpad=-260, self.ax.set_ylabel(self.xlabel, labelpad=-260,
rotation=0, fontsize=14) rotation=0, fontsize=14)
def make_line_grafic(self, k, b,): def make_line_grafic(self, k, b):
if min(self.x) > 0: if min(self.x) > 0:
xmin = min(self.x)*self.coefficient[0] xmin = min(self.x)*self.coefficient[0]
else: else:
@ -385,10 +412,10 @@ class Data:
xmax = max(self.x)*self.coefficient[0] xmax = max(self.x)*self.coefficient[0]
x = np.arange(xmin, xmax, (xmax-xmin)/10000) x = np.arange(xmin, xmax, (xmax-xmin)/10000)
plt.plot(x, k*x+b, label=self.caption, linewidth=2.4, plt.plot(x, k*x+b, label=self.caption, linewidth=2.4,
linestyle='-') linestyle='-', color=self.color[1])
def make_graffic(self, named_by_points=True):
def make_grafic(self, named_by_points=True):
self.make_errors()
if named_by_points: if named_by_points:
cap_point = self.caption cap_point = self.caption
line_point = None line_point = None
@ -396,6 +423,7 @@ class Data:
line_point = self.caption line_point = self.caption
cap_point = None cap_point = None
self.make_point_grafic() self.make_point_grafic()
k, b, sigma = self.approx() k, b, sigma = self.approx()
sigma[0] = abs(k*((sigma[0]/k)**2+(np.mean(self.yerr)/np.mean(self.y))**2 + sigma[0] = abs(k*((sigma[0]/k)**2+(np.mean(self.yerr)/np.mean(self.y))**2 +
(np.mean(self.xerr)/np.mean(self.x))**2)**0.5) (np.mean(self.xerr)/np.mean(self.x))**2)**0.5)
@ -405,15 +433,17 @@ class Data:
else: else:
sigma[1] = 0 sigma[1] = 0
self.make_line_grafic() self.make_line_grafic(k, b)
plt.legend() plt.legend()
if self.saving:
plt.savefig(self.saving)
return k, b, sigma return k, b, sigma
def approx(self): def approx(self):
if self.yerr[0] != 0: if self.yerr[0] != 0:
sigma_y = [1/i**2 for i in self.yerr] sigma_y = [1/i**2 for i in self.y]
else: else:
sigma_y = np.array([1 for _ in self.yerr]) sigma_y = np.array([1 for _ in self.y])
if self.through_0 == 0: if self.through_0 == 0:
def f(x, k): def f(x, k):
return k*x return k*x

View File

@ -0,0 +1 @@
,anna,anna-HP-Laptop-15-bw0xx,08.11.2022 18:47,file:///home/anna/.config/libreoffice/4;

BIN
Automation/j/Chart.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

View File

@ -1,3 +1,5 @@
"I_0,mA","U_34,mV","t,s" "I_0,mA","U_34,mV","t,s"
68.26666666666667,68.26666666666667,4096 52.78333333333333,52.78333333333333,3167
84.95,84.95,5097 69.46666666666667,69.46666666666667,4168
86.15,86.15,5169
102.83333333333333,102.83333333333333,6170

1 I_0,mA U_34,mV t,s
2 68.26666666666667 52.78333333333333 68.26666666666667 52.78333333333333 4096 3167
3 84.95 69.46666666666667 84.95 69.46666666666667 5097 4168
4 86.15 86.15 5169
5 102.83333333333333 102.83333333333333 6170