-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction_plotter.py
158 lines (120 loc) · 5.3 KB
/
function_plotter.py
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
import sys
from PySide2.QtWidgets import QApplication, QMainWindow, QWidget, QLabel, QLineEdit, QPushButton, QVBoxLayout,QMessageBox
from PySide2.QtGui import QIcon
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT
from matplotlib.figure import Figure
import numpy as np
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Function Plotter")
self.setGeometry(100, 100, 800, 600)
#set icon for the app
icon = QIcon("./function_plotter_icon.png")
self.setWindowIcon(icon)
# Create input fields
self.functionLabel = QLabel("Function: e.g: 5*x^3 + 2*x , Supported operators : + * ^ /")
self.functionInput = QLineEdit()
self.minValueLabel = QLabel("Min value of x:")
self.minValueInput = QLineEdit()
self.maxValueLabel = QLabel("Max value of x:")
self.maxValueInput = QLineEdit()
# Create plot and clear button
self.plotButton = QPushButton("Plot")
self.clearButton = QPushButton("Clear")
#create error message
self.errorMessageBox = QMessageBox()
# set button actions
self.plotButton.clicked.connect(self.plot_function)
self.clearButton.clicked.connect(self.clearInput)
# Create layout for input fields and the buttons
self.layout = QVBoxLayout()
self.layout.addWidget(self.functionLabel)
self.layout.addWidget(self.functionInput)
self.layout.addWidget(self.minValueLabel)
self.layout.addWidget(self.minValueInput)
self.layout.addWidget(self.maxValueLabel)
self.layout.addWidget(self.maxValueInput)
self.layout.addWidget(self.plotButton)
self.layout.addWidget(self.clearButton)
# Create main widget and set layout
self.mainWidget = QWidget()
self.mainWidget.setLayout(self.layout)
# Set main widget as central widget of main window
self.setCentralWidget(self.mainWidget)
#create canvas for plotting layout
self.fig = Figure()
self.ax = self.fig.add_subplot()
self.canvas = FigureCanvas(self.fig)
self.toolbar = NavigationToolbar2QT(self.canvas, self)
self.layout.addWidget(self.toolbar)
self.layout.addWidget(self.canvas)
def plot_function(self):
#take values from input fields
functionStr = self.functionInput.text()
functionStr = functionStr.replace("**","y").replace("^", "**")
minValueStr = self.minValueInput.text()
maxValueStr = self.maxValueInput.text()
# handle constant function
if('x' not in functionStr and len(functionStr)>0):
functionStr+='+0*x'
# Validate input values
try:
if functionStr=="":
raise ValueError("You must Enter the function before plot")
if minValueStr=="" or maxValueStr=="":
raise ValueError("You must Enter min and max value for the function")
try:
minValue = float(minValueStr)
maxValue = float(maxValueStr)
except Exception as e:
raise ValueError("min and max value must be numbers only")
if minValue >= maxValue:
raise ValueError("Minimum value must be less than maximum value")
except ValueError as e:
self.errorHandler(e)
return
try:
# Create array of x values
x = np.linspace(minValue, maxValue, 100)
# Evaluate function y for each x value and show error if function not correct
try:
y = eval(functionStr)
except Exception as e:
raise ValueError("please enter valid function")
# Create Matplotlib figure and plot function
self.fig.clear()
self.ax = self.fig.add_subplot()
self.ax.plot(x, y)
self.ax.set_xlabel("x")
self.ax.set_ylabel("y")
self.ax.set_title("Function Plot")
self.ax.axhline(y=0, color='black')
self.ax.axvline(x=0, color='black')
ymin = min(y)
ymax = max(y)
self.ax.grid(True)
self.ax.set_xlim(minValue,maxValue)
self.ax.set_ylim(ymin, ymax)
# show the plot in the main window
self.canvas.draw()
except Exception as e:
self.errorHandler(e)
def clearInput(self):
#clear input fields and the plot also
self.functionInput.clear()
self.minValueInput.clear()
self.maxValueInput.clear()
self.ax.clear()
self.canvas.draw()
def errorHandler(self, e):
# Display error message for any error
self.errorMessageBox.setWindowTitle("Error!")
self.errorMessageBox.setText(str(e))
self.errorMessageBox.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
sys.exit(app.exec_())