-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviolin_plot.py
More file actions
121 lines (95 loc) · 4 KB
/
Copy pathviolin_plot.py
File metadata and controls
121 lines (95 loc) · 4 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
try:
__import__('PySide6')
__import__('matplotlib')
except ImportError as e:
print(f"Please install the missing package: {e.name}")
from PySide6.QtWidgets import (
QApplication, QMainWindow, QVBoxLayout, QWidget,
QPushButton, QTableView, QLineEdit, QHBoxLayout,
QMessageBox, QFormLayout
)
from PySide6.QtGui import QStandardItemModel, QStandardItem
from PySide6.QtCore import Qt
import matplotlib.pyplot as plt
import matplotlib
import sys
matplotlib.use('Qt5Agg')
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Measurement Uncertainty and Distribution Plotter")
self.setGeometry(100, 100, 800, 600)
layout = QVBoxLayout()
self.table_view = QTableView()
self.model = QStandardItemModel()
self.model.setHorizontalHeaderLabels(['Measurement', 'Accurate Value'])
self.table_view.setModel(self.model)
self.add_button = QPushButton("Add Measurement")
self.add_button.clicked.connect(self.add_measurement)
self.measurement_input = QLineEdit()
self.accurate_value_input = QLineEdit()
form_layout = QFormLayout()
form_layout.addRow("Measurement:", self.measurement_input)
form_layout.addRow("Accurate Value:", self.accurate_value_input)
self.plot_button = QPushButton("Plot Data")
self.plot_button.clicked.connect(self.plot_data)
layout.addLayout(form_layout)
layout.addWidget(self.add_button)
layout.addWidget(self.table_view)
layout.addWidget(self.plot_button)
container = QWidget()
container.setLayout(layout)
self.setCentralWidget(container)
def add_measurement(self):
measurement_text = self.measurement_input.text()
accurate_value_text = self.accurate_value_input.text()
if not measurement_text or not accurate_value_text:
QMessageBox.warning(self, "Input Error", "Both fields must be filled")
return
try:
measurement = float(measurement_text)
accurate_value = float(accurate_value_text)
except ValueError:
QMessageBox.warning(self, "Input Error", "Invalid input. Please enter numeric values.")
return
self.model.appendRow([
QStandardItem(measurement_text),
QStandardItem(accurate_value_text)
])
self.measurement_input.clear()
self.accurate_value_input.clear()
def plot_data(self):
measurements = []
accurate_values = []
for row in range(self.model.rowCount()):
try:
measurement = float(self.model.item(row, 0).text())
accurate_value = float(self.model.item(row, 1).text())
measurements.append(measurement)
accurate_values.append(accurate_value)
except ValueError:
QMessageBox.warning(self, "Input Error", f"Invalid data at row {row+1}")
return
if not measurements or not accurate_values:
QMessageBox.warning(self, "Input Error", "No data to plot")
return
fig, ax = plt.subplots(nrows=2, ncols=1, figsize=(10, 8))
# Candlestick plot
ax[0].boxplot(measurements, vert=True, patch_artist=True)
ax[0].set_title('Candlestick Plot of Measurements')
ax[0].set_ylabel('Measurements')
# Violin plot
parts = ax[1].violinplot(measurements, showmeans=False, showmedians=True)
for pc in parts['bodies']:
pc.set_facecolor('lightblue')
pc.set_edgecolor('black')
pc.set_alpha(0.7)
ax[1].set_title('Violin Plot of Measurements')
ax[1].set_ylabel('Measurements')
plt.tight_layout()
plt.show()
app = QApplication(sys.argv)
app.setStyle('Fusion')
window = MainWindow()
window.show()
sys.exit(app.exec())