summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Guschin <saintruler@gmail.com>2021-04-05 19:52:58 +0400
committerAndrew Guschin <saintruler@gmail.com>2021-04-05 19:52:58 +0400
commit430a599dcd9454f95c85ab6eb6b72eb15d584b92 (patch)
tree135a73ea7ae0f0b65d19636485cca8de81c8945f
parent399b82de92f508b8d96ea0c55d240ee09e0310b3 (diff)
Добавил возможность выводить предустановленные и поточечные графики
-rw-r--r--.gitignore1
-rw-r--r--python-graphics/main.py53
-rw-r--r--python-graphics/plotter.py60
3 files changed, 106 insertions, 8 deletions
diff --git a/.gitignore b/.gitignore
index f0da146..e0a97e5 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
*.out
*.zip
venv
+__pycache__
diff --git a/python-graphics/main.py b/python-graphics/main.py
index 99ec770..fabdd92 100644
--- a/python-graphics/main.py
+++ b/python-graphics/main.py
@@ -2,6 +2,8 @@ import tkinter as tk
from tkinter import filedialog as fd
from tkinter import ttk
+from plotter import plot_preset, plot_points
+
class PlotType:
USER = 1
@@ -9,6 +11,15 @@ class PlotType:
POINT = 3
+class GraphMode:
+ LINES = "Линии"
+ DOTS = "Точки"
+
+ @classmethod
+ def options(cls):
+ return [cls.DOTS, cls.LINES]
+
+
class PresetType:
LINE = "ax + b = 0"
PARABOLA = "ax^2 + bx + c = 0"
@@ -131,25 +142,25 @@ class PresetPlotFrame(tk.Frame):
cur_type = self.preset_type.get()
v = self.variables[cur_type]
try:
- min_x = float(self.min.get())
+ min_x = float(self.min.get().strip())
except ValueError:
return {"status": "error", "message": "min(x) не является float"}
try:
- max_x = float(self.max.get())
+ max_x = float(self.max.get().strip())
except ValueError:
return {"status": "error", "message": "max(x) не является float"}
try:
- a = float(v[0][1].get())
+ a = float(v[0][1].get().strip())
except ValueError:
return {"status": "error", "message": "a не является float"}
try:
- b = float(v[1][1].get())
+ b = float(v[1][1].get().strip())
except ValueError:
return {"status": "error", "message": "b не является float"}
if cur_type == PresetType.PARABOLA:
try:
- c = float(v[2][1].get())
+ c = float(v[2][1].get().strip())
except ValueError:
return {"status": "error", "message": "c не является float"}
return {
@@ -188,7 +199,7 @@ class UserPlotFrame(tk.Frame):
self.grid(row=0, column=0, columnspan=1, rowspan=2, sticky=tk.NSEW)
def collect_info(self):
- return {"status": "ok"}
+ return {"status": "ok", "graph": "user"}
class PointPlotFrame(tk.Frame):
@@ -199,6 +210,7 @@ class PointPlotFrame(tk.Frame):
self.show()
self.create_widgets()
self._path = ""
+ self.mode = "o"
def create_widgets(self):
self.choose_file = tk.Button(self)
@@ -209,6 +221,25 @@ class PointPlotFrame(tk.Frame):
self.filepath = tk.Entry(self)
self.filepath.grid(row=1, column=0, sticky=tk.NSEW)
+ # Изменения этой переменной будут передаваться в метод type_changed
+ self.mode_var = tk.StringVar()
+ self.mode_var.trace("w", self.mode_changed)
+
+ self.options = ttk.OptionMenu(
+ self,
+ self.mode_var,
+ GraphMode.DOTS,
+ *GraphMode.options(),
+ )
+ self.options.grid(row=2, column=0, columnspan=1, rowspan=1, sticky=tk.NSEW)
+
+ def mode_changed(self, *args):
+ mode = self.mode_var.get()
+ if mode == GraphMode.DOTS:
+ self.mode = "o"
+ elif mode == GraphMode.LINES:
+ self.mode = "r"
+
def open_file(self):
file_handle = tk.filedialog.askopenfile()
if file_handle is not None:
@@ -236,7 +267,7 @@ class PointPlotFrame(tk.Frame):
except ValueError:
return {"status": "error", "message": "Неверный формат данных"}
- return {"status": "ok", "graph": "point", "points": points}
+ return {"status": "ok", "graph": "point", "mode": self.mode, "points": points}
class App(tk.Frame):
@@ -313,7 +344,13 @@ class App(tk.Frame):
self.error_label.grid(row=7, column=0, rowspan=1, columnspan=2, padx=10, pady=5, sticky=tk.NSEW)
else:
self.error_label.grid_forget()
- print(info)
+
+ if info["graph"] == "preset":
+ plot_preset(info)
+ elif info["graph"] == "point":
+ plot_points(info)
+ else:
+ print(info)
def grid_conf(obj, rows, cols, uniform=False):
diff --git a/python-graphics/plotter.py b/python-graphics/plotter.py
new file mode 100644
index 0000000..b2bc029
--- /dev/null
+++ b/python-graphics/plotter.py
@@ -0,0 +1,60 @@
+import matplotlib.pyplot as plt
+import numpy as np
+
+
+def plot_preset(data):
+ a = data["a"]
+ b = data["b"]
+ min_x = data["min"]
+ max_x = data["max"]
+
+ # 100 linearly spaced numbers
+ x = np.linspace(min_x, max_x, 100)
+
+ if data["type"] == "parabola":
+ c = data["c"]
+ y = a * x**2 + b * x + c
+ elif data["type"] == "line":
+ y = a * x + b
+
+ # setting the axes at the centre
+ fig = plt.figure()
+ ax = fig.add_subplot(1, 1, 1)
+ ax.spines['left'].set_position('center')
+ ax.spines['bottom'].set_position('zero')
+ ax.spines['right'].set_color('none')
+ ax.spines['top'].set_color('none')
+ ax.xaxis.set_ticks_position('bottom')
+ ax.yaxis.set_ticks_position('left')
+
+ # plot the function
+ plt.plot(x,y, 'r')
+
+ # show the plot
+ plt.show()
+
+
+def plot_points(data):
+ points = data["points"]
+
+ x = []
+ y = []
+ for (xi, yi) in points:
+ x.append(xi)
+ y.append(yi)
+
+ # setting the axes at the centre
+ fig = plt.figure()
+ ax = fig.add_subplot(1, 1, 1)
+ ax.spines['left'].set_position('center')
+ ax.spines['bottom'].set_position('zero')
+ ax.spines['right'].set_color('none')
+ ax.spines['top'].set_color('none')
+ ax.xaxis.set_ticks_position('bottom')
+ ax.yaxis.set_ticks_position('left')
+
+ # plot the function
+ plt.plot(x,y, data["mode"])
+
+ # show the plot
+ plt.show()