-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualization_widget.py
More file actions
105 lines (85 loc) · 3.71 KB
/
visualization_widget.py
File metadata and controls
105 lines (85 loc) · 3.71 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
from PySide6.QtCore import Qt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import seaborn as sns
from PySide6.QtWidgets import QWidget, QVBoxLayout, QPushButton, QLabel, QTableWidget, QTableWidgetItem, QSplitter
class VisualizationWidget(QSplitter):
def __init__(self, data_manager, parent=None):
super().__init__(Qt.Vertical, parent)
self.data_manager = data_manager
# Table widget
self.table_widget = TableWidget(self.data_manager, self)
self.addWidget(self.table_widget)
# Graph widget
self.graph_widget = GraphWidget(self.data_manager, self)
self.addWidget(self.graph_widget)
class TableWidget(QTableWidget):
def __init__(self, data_manager, parent=None):
super().__init__(parent)
self.data_manager = data_manager
self.data_manager.data_changed.connect(self.update_table)
def update_table(self):
"""
Update the table with the latest data from the data manager.
No returns or feedback is provided
"""
data = self.data_manager.get_df()
if data is None or data.empty:
self.setRowCount(0)
self.setColumnCount(0)
return
# Set the number of rows and columns
self.setRowCount(len(data))
self.setColumnCount(len(data.columns))
# Set the column headers
self.setHorizontalHeaderLabels(data.columns)
# Populate the table with data
for row_idx, row in data.iterrows():
for col_idx, value in enumerate(row):
self.setItem(row_idx, col_idx, QTableWidgetItem(str(value)))
class GraphWidget(QWidget):
def __init__(self, data_manager, parent=None):
super().__init__(parent)
self.data_manager = data_manager
self.layout = QVBoxLayout(self)
# Plot figure and canvas
self.figure = Figure()
self.canvas = FigureCanvas(self.figure)
self.layout.addWidget(self.canvas)
# Label to display feedback
self.feedback_label = QLabel("Load data to plot a scatter plot.")
# Fix the height of the label
self.feedback_label.setFixedHeight(self.feedback_label.fontMetrics().height())
self.layout.addWidget(self.feedback_label)
# Button to plot the graph
self.plot_button = QPushButton("Plot Scatter Plot", self)
self.plot_button.clicked.connect(self.plot_scatter)
self.layout.addWidget(self.plot_button)
def plot_scatter(self):
"""Plot a scatter plot
Data is loaded from `self.data_manager` to plot the plot.
Feedback label for the plotting will be updated to provide user feedback.
"""
data = self.data_manager.get_df()
if data is None:
self.feedback_label.setText("No data loaded.")
return
# Check if there are at least two columns for plotting
if len(data.columns) < 2:
self.feedback_label.setText("CSV must have at least two columns for plotting.")
return
try:
# Clear the figure
self.figure.clear()
# Use the first two columns for the scatter plot
x_col = data.columns[0]
ax = self.figure.add_subplot(111)
if len(data.columns) > 2:
ax.set_ylabel("Values")
for y_col in data.columns[1:]:
sns.scatterplot(data=data, x=x_col, y=y_col, ax=ax, label=y_col)
# Draw the plot on the canvas
self.canvas.draw()
self.feedback_label.setText("Scatter plot updated.")
except Exception as e:
self.feedback_label.setText(f"Error plotting scatter plot: {e}")