hbui
2024-07-22 1140a81f6f2087def8194ca9986f393417d17707
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
import sys
import matplotlib
 
from PySide6.QtWidgets import QApplication, QMainWindow, QVBoxLayout
 
from numint.input_parser import parse_function, parse_value
from numint.riemann_sum import numint_epsilon, numint_section
from numint.ui_mainwindow import Ui_MainWindow
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg, NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
 
matplotlib.use('QtAgg')
 
 
class MplCanvas(FigureCanvasQTAgg):
    def __init__(self, parent=None, width=5, height=4, dpi=100):
        fig = Figure(figsize=(width, height), dpi=dpi)
        self.axes = fig.add_subplot(111)
        super(MplCanvas, self).__init__(fig)
 
 
class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.canvas = MplCanvas(self)
        self._update_plot()
 
        toolbar = NavigationToolbar(self.canvas, self)
        layout = QVBoxLayout()
        layout.addWidget(toolbar)
        layout.addWidget(self.canvas)
        self.ui.plotBox.setLayout(layout)
        self.ui.plotBtn.clicked.connect(self._update_plot)
 
    def _update_plot(self):
        fn_expr = self.ui.function.text()
        self.fn_expr = fn_expr
        start = self.ui.startValue.text()
        end = self.ui.endValue.text()
        epsilon = self.ui.espilon.text()
        try:
            f = parse_function(fn_expr)
            (a, b) = (parse_value(start), parse_value(end))
            if epsilon is not None and len(epsilon.strip()) > 0:
                eps = parse_value(epsilon)
                print(f"plot f(x) = {fn_expr}, {a}, {b}, epsilon = {eps}")
                (self.x, self.y, *_) = numint_epsilon(f, a, b, eps)
                self._update_plot_eps()
            else:
                section = self.ui.section.text()
                sec = parse_value(section)
                print(f"plot f(x) = {fn_expr}, {a}, {b}, section = {sec}")
                (self.x, self.y, *_) = numint_section(f, a, b, sec)
                self._update_plot_eps()
        except Exception as ex:
            print(ex)
            pass
 
    def _update_plot_eps(self):
        self.canvas.axes.cla()  # Clear the canvas.
        # self.canvas.axes.stairs(self.y[1:],   self.x, baseline=0, fill=True, alpha=0.5, label=self.fn_expr)
        # self.canvas.axes.stairs(self.y[0:-1], self.x, baseline=0, fill=True, alpha=0.5, label=self.fn_expr)
        self.canvas.axes.step(self.x, self.y, color="blue",   where = "pre",  label=self.fn_expr)
        self.canvas.axes.step(self.x, self.y, color="orange", where = "post", label=self.fn_expr)
        self.canvas.axes.plot(self.x, self.y, color="black")
        self.canvas.draw()
 
 
 
def main():
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
 
    sys.exit(app.exec())
 
 
if __name__ == "__main__":
    main()