|
1 | | -from PyQt6.QtWidgets import QVBoxLayout, QWidget |
2 | | -from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas |
3 | | -from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT |
4 | | -import matplotlib.pyplot as plt |
5 | | -import numpy as np |
6 | | -from numpy import sin, cos, tan |
7 | | -from mpl_interactions import panhandler, zoom_factory |
8 | | - |
9 | | - |
10 | | -class MatplotlibWidget(QWidget): |
11 | | - def __init__(self, parent=None): |
12 | | - super().__init__(parent) |
13 | | - |
14 | | - self.figure, self.axis = plt.subplots() |
15 | | - self.canvas = FigureCanvas(self.figure) |
16 | | - layout = QVBoxLayout() |
17 | | - self.parent = parent |
18 | | - layout.addWidget(self.canvas) |
19 | | - self.setLayout(layout) |
20 | | - |
21 | | - self.toolbar = NavigationToolbar2QT(self.canvas, self) |
22 | | - |
23 | | - # Add interactive features for panning and zooming |
24 | | - plt.ioff() # Turn interactive mode off |
25 | | - self.canvas.mpl_connect("draw_event", panhandler) |
26 | | - zoom_factory(self.axis, base_scale=1.1) |
27 | | - |
28 | | - def plot_function(self, functions): |
29 | | - self.axis.clear() |
30 | | - try: |
31 | | - x = np.linspace(-20, 20, 400) |
32 | | - for function_text in functions: |
33 | | - y = eval(function_text) |
34 | | - self.axis.plot(x, y, label=function_text) # Add a label for each function |
35 | | - plt.grid() |
36 | | - self.axis.legend() # Add legend to the plot |
37 | | - self.canvas.draw() |
38 | | - self.parent.function_list_widget.addItem(function_text) |
39 | | - except Exception as e: |
40 | | - print("Error:", e) |
| 1 | +import re |
| 2 | + |
| 3 | +from PyQt6.QtCore import Qt |
| 4 | +from PyQt6.QtWidgets import QVBoxLayout, QWidget |
| 5 | +from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas |
| 6 | +from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT |
| 7 | +import matplotlib.pyplot as plt |
| 8 | +import numpy as np |
| 9 | + |
| 10 | +# math functions |
| 11 | +from numpy import sin, cos, tan, sqrt, arcsin, arccos, arctan |
| 12 | + |
| 13 | +from mpl_interactions import panhandler, zoom_factory |
| 14 | + |
| 15 | +top = 0.977 |
| 16 | +bottom = 0.029 |
| 17 | +left = 0.028 |
| 18 | +right = 1.0 |
| 19 | +hspace = 0.2 |
| 20 | +wspace = 0.2 |
| 21 | + |
| 22 | + |
| 23 | +class MatplotlibWidget(QWidget): |
| 24 | + def __init__(self, parent=None): |
| 25 | + super().__init__(parent) |
| 26 | + |
| 27 | + self.figure, self.axis = plt.subplots() |
| 28 | + self.figure.patch.set_facecolor('#202124') |
| 29 | + plt.tight_layout() |
| 30 | + plt.subplots_adjust(left=left, right=right, top=top, bottom=bottom, hspace=hspace, wspace=wspace) |
| 31 | + self.canvas = FigureCanvas(self.figure) |
| 32 | + |
| 33 | + # Set margins and spacing of the layout to 0 |
| 34 | + layout = QVBoxLayout(self) |
| 35 | + layout.setContentsMargins(0, 0, 0, 0) |
| 36 | + layout.setSpacing(0) |
| 37 | + layout.addWidget(self.canvas) |
| 38 | + |
| 39 | + self.toolbar = NavigationToolbar2QT(self.canvas, self) |
| 40 | + |
| 41 | + # Add interactive features for panning and zooming |
| 42 | + plt.ioff() # Turn interactive mode off |
| 43 | + plt.grid() |
| 44 | + self.axis.set_facecolor('#202124') |
| 45 | + self.canvas.mpl_connect("draw_event", panhandler) |
| 46 | + |
| 47 | + zoom_factory(self.axis, base_scale=1.1) |
| 48 | + |
| 49 | + |
| 50 | + def plot_function(self, functions): |
| 51 | + self.axis.clear() |
| 52 | + try: |
| 53 | + x = np.linspace(-1, 1, 100) # Adjust the number of points for smoother plots |
| 54 | + for function_text in functions: |
| 55 | + # Replace '2x' with '2*x' for multiplication |
| 56 | + function_text = re.sub(r'(\d+)([a-zA-Z])', r'\1*\2', function_text) |
| 57 | + # Replace other symbols as needed, for example '^' with '**' |
| 58 | + function_text = function_text.replace('^', '**') |
| 59 | + # Add more replacements as necessary |
| 60 | + |
| 61 | + if '=' in function_text: |
| 62 | + variable, expression = function_text.split('=') |
| 63 | + variable = variable.strip() |
| 64 | + expression = expression.strip() |
| 65 | + locals()[variable] = eval(expression) |
| 66 | + else: |
| 67 | + y = eval(function_text) |
| 68 | + self.axis.plot(x, y, label=function_text) # Add a label for each function |
| 69 | + |
| 70 | + plt.grid() |
| 71 | + self.axis.legend() # Add legend to the plot |
| 72 | + self.canvas.draw() |
| 73 | + # self.parent.function_list_widget.addItem(function_text) |
| 74 | + except Exception as e: |
| 75 | + print("Error:", e) |
0 commit comments