-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_outside.py
More file actions
223 lines (178 loc) · 7.47 KB
/
plot_outside.py
File metadata and controls
223 lines (178 loc) · 7.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QWidget, QComboBox
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
import os
import sys
from fit import Fit
matplotlib.use('qt5Agg')
plt.rcParams['font.family'] = 'serif'
plt.rcParams['font.serif'] = 'Palatino'
plt.rcParams['text.usetex'] = True
plt.rcParams['text.latex.unicode'] = True
plt.rcParams.update({'font.size': 28})
plt.rcParams['text.latex.preamble'] = r'\usepackage[T1]{polski}'
class Data_to_plot:
x = 0
y = 0
z = 0
xlabel = "x"
ylabel = "y"
title = ""
class Window(QtWidgets.QMainWindow, Data_to_plot):
def __init__(self, parent=None):
super().__init__(parent)
wid = QWidget(self)
self.setCentralWidget(wid)
self.button_to_plot = QtWidgets.QPushButton('Plot')
self.button_to_plot.clicked.connect(self.plot)
self.button_to_plot_twinx = QtWidgets.QPushButton('Plot twinx')
self.button_to_plot_twinx.clicked.connect(self.plot_twinx)
self.button_to_reset = QtWidgets.QPushButton('Reset')
self.button_to_reset.clicked.connect(self.reset)
self.button_to_home = QtWidgets.QPushButton('Home')
self.button_to_home.clicked.connect(self.home)
self.button_to_save_fig = QtWidgets.QPushButton('Save')
self.button_to_save_fig.clicked.connect(self.save_fig)
self.button_to_set_xlabel = QtWidgets.QPushButton('Set xlabel')
self.button_to_set_xlabel.clicked.connect(self.set_xlabel)
self.text_xlable = QtWidgets.QLineEdit()
self.button_to_set_ylabel = QtWidgets.QPushButton('Set ylabel')
self.button_to_set_ylabel.clicked.connect(self.set_ylabel)
self.text_ylable = QtWidgets.QLineEdit()
self.button_to_set_title = QtWidgets.QPushButton('Set title')
self.button_to_set_title.clicked.connect(self.set_title)
self.text_title = QtWidgets.QLineEdit()
self.button_to_import_data = QtWidgets.QPushButton('import data x,y')
self.button_to_import_data.clicked.connect(self.import_data)
self.textEdit = QtWidgets.QTextEdit()
self.button_to_import_data_xyz = QtWidgets.QPushButton('import data x,y,z')
self.button_to_import_data_xyz.clicked.connect(self.import_data_xyz)
self.label_fit = QtWidgets.QLabel('Fit')
self.combo_to_chose_poly = QComboBox(self)
self.combo_to_chose_poly.addItem("poly1")
self.combo_to_chose_poly.addItem("poly2")
self.fit_info = QtWidgets.QLineEdit()
self.fit_info.setEnabled(True)
self.combo_to_chose_poly.activated[str].connect(self.fit)
#set the layout
layout = QtWidgets.QVBoxLayout()
btnlayout1 = QtWidgets.QHBoxLayout()
btnlayout1.addWidget(self.button_to_plot)
btnlayout1.addWidget(self.button_to_plot_twinx)
btnlayout1.addWidget(self.button_to_reset)
btnlayout1.addWidget(self.button_to_home)
btnlayout1.addWidget(self.button_to_save_fig)
btnlayout2 = QtWidgets.QHBoxLayout()
btnlayout2.addWidget(self.button_to_set_xlabel)
btnlayout2.addWidget(self.button_to_set_ylabel)
btnlayout2.addWidget(self.button_to_set_title)
btnlayout3 = QtWidgets.QHBoxLayout()
btnlayout3.addWidget(self.text_xlable)
btnlayout3.addWidget(self.text_ylable)
btnlayout3.addWidget(self.text_title)
btnlayout4 = QtWidgets.QHBoxLayout()
btnlayout4.addWidget(self.button_to_import_data)
btnlayout4.addWidget(self.textEdit)
btnlayout4.addWidget(self.button_to_import_data_xyz)
btnlayout5 = QtWidgets.QHBoxLayout()
btnlayout5.addWidget(self.label_fit)
btnlayout5.addWidget(self.combo_to_chose_poly)
btnlayout5.addWidget(self.fit_info)
qw = QtWidgets.QWidget(self)
qw.setLayout(btnlayout1)
qw2 = QtWidgets.QWidget(self)
qw2.setLayout(btnlayout2)
qw3 = QtWidgets.QWidget(self)
qw3.setLayout(btnlayout3)
qw4 = QtWidgets.QWidget(self)
qw4.setLayout(btnlayout4)
qw5 = QtWidgets.QWidget(self)
qw5.setLayout(btnlayout5)
layout.addWidget(qw)
layout.addWidget(qw2)
layout.addWidget(qw3)
layout.addWidget(qw5)
layout.addWidget(qw4)
wid.setLayout(layout)
self.setGeometry(800, 900, 600, 700)
def home(self):
pass
def reset(self):
plt.close("all")
def import_data(self):
fname = QtWidgets.QFileDialog.getOpenFileName(self, 'Open file', os.getcwd())
if fname[0]:
try:
self.x, self.y = np.loadtxt(fname[0], unpack=True, skiprows=1)
with open(fname[0], 'r') as f:
data = f.read()
self.textEdit.setText(data)
except Exception as err:
print(err)
def import_data_xyz(self):
fname = QtWidgets.QFileDialog.getOpenFileName(self, 'Open file', os.getcwd())
if fname[0]:
try:
self.x, self.y, self.z = np.loadtxt(fname[0], unpack=True, skiprows=1)
with open(fname[0], 'r') as f:
data = f.read()
self.textEdit.setText(data)
except Exception as err:
print(err)
def plot(self):
self.fig, self.axes = plt.subplots()
self.axes.plot(self.x, self.y, 'bo', markersize=10)
plt.grid(True)
plt.show()
def plot_twinx(self):
self.fig, self.axes = plt.subplots()
self.axes.plot(self.x, self.y, 'bo', label="prąd [A]", markersize=14)
self.axes2 = self.axes.twinx()
self.axes2.plot(self.x, self.z, 'r<', label="moc [W]", markersize=14)
self.axes2.set_ylabel("moc [W]")
box = self.axes.get_position()
self.axes.set_position([box.x0, box.y0, box.width * 0.8, box.height])
self.axes2.set_position([box.x0, box.y0, box.width * 0.8, box.height])
self.axes.legend(loc='center left', bbox_to_anchor=(1.05, 0.8), frameon=False)
self.axes2.legend(loc='upper left', bbox_to_anchor=(1.05, 0.8), frameon=False)
plt.grid(True)
plt.show()
def set_title(self):
self.title = self.text_title.text()
self.axes.set_title(self.title)
self.fig.canvas.draw()
def set_xlabel(self):
self.xlabel = self.text_xlable.text()
self.axes.set_xlabel(self.xlabel)
self.fig.canvas.draw()
def set_ylabel(self):
self.ylabel = self.text_ylable.text()
self.axes.set_ylabel(self.ylabel)
self.fig.canvas.draw()
def save_fig(self):
path_to_save_fig = os.path.join(os.getcwd(), "wykres.jpg")
plt.savefig(path_to_save_fig)
def fit(self, text):
f = Fit(self.x, self.y)
if text=="poly1":
a, b, da, db = f.do_fit_by_poly1()
self.fit_info.setText("a={}; b={}".format(a, b))
x = np.linspace(min(self.x), max(self.x), 1000)
y = a*x + b
self.axes.plot(x, y, 'r-', lw=2)
self.fig.canvas.draw()
if text == "poly2":
a, b, c, da, db, dc = f.do_fit_by_poly2()
self.fit_info.setText("a={}; b={}; c={}".format(a, b, c))
x = np.linspace(min(self.x), max(self.x), 1000)
y = a*x*x + b*x + c
self.axes.plot(x, y, 'g-', lw=2)
self.fig.canvas.draw()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
main = Window()
main.setWindowTitle('PyPlotGui')
main.show()
sys.exit(app.exec_())