16
16
17
17
18
18
class FunctionPlotter (QMainWindow ):
19
+ """
20
+ A class representing the main window for the function plotter application.
21
+
22
+ This class handles the GUI elements, user inputs, and plotting of mathematical functions
23
+ using PySide2 for the GUI and Matplotlib for plotting.
24
+ """
25
+
19
26
def __init__ (self ):
20
27
super ().__init__ ()
21
28
self .setWindowTitle ("Function Plotter" )
22
29
23
- # Central widget
30
+ # Initialize the central widget and main layout
24
31
self .central_widget = QWidget ()
25
32
self .setCentralWidget (self .central_widget )
26
-
27
- # Layouts
28
33
self .layout = QVBoxLayout ()
29
34
self .input_layout = QHBoxLayout ()
30
35
self .central_widget .setLayout (self .layout )
31
36
32
- # Function input
37
+ # Initialize input fields for the function and range values
33
38
self .function_input = QLineEdit ()
34
39
self .function_input .setPlaceholderText ("Enter function of x, e.g., 5*x^3 + 2*x" )
35
40
self .input_layout .addWidget (self .function_input )
36
41
37
- # Min and max inputs
38
42
self .min_input = QLineEdit ()
39
43
self .min_input .setPlaceholderText ("Enter min value of x" )
40
44
self .input_layout .addWidget (self .min_input )
@@ -45,19 +49,21 @@ def __init__(self):
45
49
46
50
self .layout .addLayout (self .input_layout )
47
51
48
- # Plot button
52
+ # Initialize the plot button
49
53
self .plot_button = QPushButton ("Plot Function" )
50
54
self .plot_button .clicked .connect (self .plot_function )
51
55
self .layout .addWidget (self .plot_button )
52
56
53
- # Matplotlib figure
57
+ # Initialize the Matplotlib figure and canvas
54
58
self .figure , self .ax = plt .subplots ()
55
59
self .canvas = FigureCanvas (self .figure )
56
60
self .layout .addWidget (self .canvas )
57
61
58
62
def plot_function (self ):
59
- function = self .function_input .text ()
60
- function = function .replace (" " , "" )
63
+ """
64
+ Handles the plotting of the function based on user inputs.
65
+ """
66
+ function = self .function_input .text ().replace (" " , "" )
61
67
min_x = self .min_input .text ()
62
68
max_x = self .max_input .text ()
63
69
@@ -67,7 +73,12 @@ def plot_function(self):
67
73
68
74
try :
69
75
min_x = float (min_x )
76
+ # Make range from -1e20 to 1e20 whatever the input
77
+ min_x = max (min (1e20 , min_x ), - 1e20 )
70
78
max_x = float (max_x )
79
+ # Make range from -1e20 to 1e20 whatever the input
80
+ max_x = max (min (1e20 , max_x ), - 1e20 )
81
+
71
82
except ValueError :
72
83
self .show_error_message ("Min and Max values must be numbers." )
73
84
return
@@ -89,7 +100,7 @@ def plot_function(self):
89
100
self .show_error_message (f"Error in function evaluation: { e } " )
90
101
return
91
102
92
- # Clear previous plot
103
+ # Clear previous plot and plot new function
93
104
self .ax .clear ()
94
105
self .ax .plot (x , y )
95
106
self .ax .set_title (f"Plot of { function } " )
@@ -98,6 +109,17 @@ def plot_function(self):
98
109
self .canvas .draw ()
99
110
100
111
def validate_inputs (self , function , min_x , max_x ):
112
+ """
113
+ Validates the user inputs for the function and range values.
114
+
115
+ Parameters:
116
+ - function (str): The mathematical function entered by the user.
117
+ - min_x (str): The minimum value of x entered by the user.
118
+ - max_x (str): The maximum value of x entered by the user.
119
+
120
+ Returns:
121
+ - bool: True if inputs are valid, False otherwise.
122
+ """
101
123
if not function :
102
124
self .show_error_message ("Function cannot be empty." )
103
125
return False
@@ -113,12 +135,31 @@ def validate_inputs(self, function, min_x, max_x):
113
135
return True
114
136
115
137
def prepare_function (self , function , x ):
138
+ """
139
+ Prepares the function for evaluation by replacing operators with their numpy equivalents.
140
+
141
+ Parameters:
142
+ - function (str): The mathematical function entered by the user.
143
+ - x (np.ndarray): The array of x values.
144
+
145
+ Returns:
146
+ - str: The prepared function.
147
+ """
116
148
function = function .replace ("^" , "**" )
117
149
function = function .replace ("log10" , "np.log10" )
118
150
function = function .replace ("sqrt" , "np.sqrt" )
151
+ # if function is a constant (e.g. y=5)
152
+ if "x" not in function :
153
+ function = f"{ function } *np.ones_like(x)"
119
154
return function
120
155
121
156
def show_error_message (self , message ):
157
+ """
158
+ Displays an error message dialog.
159
+
160
+ Parameters:
161
+ - message (str): The error message to display.
162
+ """
122
163
QMessageBox .critical (self , "Error" , message )
123
164
124
165
0 commit comments