Просмотр исходного кода

Создание анимации для графика

Lev_Langovskii 1 месяц назад
Родитель
Сommit
c13c61b059
5 измененных файлов с 2051 добавлено и 21 удалено
  1. 0 0
      1111tttt.py
  2. 51 21
      InterfaceL.py
  3. 1000 0
      coordinates.txt
  4. 1000 0
      coordinates1.txt
  5. 0 0
      generatorT.py

+ 0 - 0
1111tttt.py


+ 51 - 21
InterfaceL.py

@@ -12,7 +12,8 @@ import matplotlib
 import matplotlib.animation as animation
 import numpy as np
 import os
-
+from matplotlib.animation import FuncAnimation
+import tkinter as tk
 
 
 class Interface():
@@ -124,23 +125,38 @@ class Interface():
 
 
     def create_Figure(self):
-        self.file1 = open("1234test.txt", 'r')
-        self.data = self.file1.readlines()
-        self.mnojtel = int(self.data[0])
+        self.file1 = open('coordinates.txt', 'r')
+        with self.file1 as f:
+            self.lines = f.readlines()
+        self.data = [line.strip().split() for line in self.lines]
+        self.xdata, self.ydata = zip(*[(float(x), float(y)) for x, y in self.data])
         self.f_graf = ttk.LabelFrame(Interface.window,
                                      text='Количество импульсов в минуту', relief=SOLID)
         self.f_graf.grid(column=1, row=1, columnspan=2, rowspan=2)
         self.fig = Figure(figsize=(7, 4), dpi=80)
-        self.t = np.linspace(0, 100, num=10000)
         self.ax = self.fig.add_subplot(111)
         self.ax.set_xlabel('Время, с')
         self.ax.set_ylabel('Импульсы')
-        self.ax.plot(self.t, 5000 + 5000 * np.sin(self.t / self.mnojtel), '-rh',
-                     linewidth=1, markersize=1, markerfacecolor='b')
         self.ax.grid(color='black', linewidth=1.0)
+        self.ax.set_xlim(min(self.xdata), max(self.xdata))
+        self.ax.set_ylim(min(self.ydata), max(self.ydata))
         self.canvas = FigureCanvasTkAgg(self.fig, master=self.f_graf)
         self.canvas.draw()
         self.canvas.get_tk_widget().grid(column=0, row=0)
+        self.line, = self.ax.plot([], [], 'ro-')
+
+        def init():
+            self.line.set_data([], [])
+            return self.line,
+
+        def update(frame):
+            self.line.set_data(self.xdata[:frame], self.ydata[:frame])
+            return self.line,
+
+        self.ani = FuncAnimation(self.fig, update, frames=len(self.xdata),
+                            init_func=init, blit=True, interval=5, repeat=False)
+
+
         Interface.window.after(100, None)
         self.f_grafbtn = ttk.LabelFrame(self.f_graf, text='Время отоброжения', padding=[8, 10])
         self.f_grafbtn.grid(column=1, row=0)
@@ -183,6 +199,7 @@ class Interface():
     def clicked_file(self):
         self.file1 = filedialog.askopenfile(filetypes=(("Text files","*.txt"),("all files","*.*")))
         self.file = os.path.abspath(self.file1.name)
+        print(self.file)
         self.txt.delete(0, END)
         self.txt.insert(0, self.file)
 
@@ -207,31 +224,44 @@ class Interface():
             self.txt1_fw3.configure(text='      0 B      ')
 
     def clicked_time(self):
+        if self.ani is not None:
+            self.ani.event_source.stop()
+            self.ani = None
         if self.Time_flag == 'disabled':
             self.Time_flag = 'normal'
             self.grafspin1.configure(state=[self.Time_flag])
         else:
             self.file1 = open(self.file, 'r')
-            self.data = self.file1.readlines()
-            self.mnojtel = int(self.data[0])
             self.Time_flag = 'disabled'
             self.grafspin1.configure(state=[self.Time_flag])
-            self.fig.clear()
-            self.fig = Figure(figsize=(7, 4), dpi=80)
-            self.ax = self.fig.add_subplot(111)
-            self.t = np.linspace(0, int(self.spinbox_varTime.get()), num=10000)
-            self.ax.set_ylim(bottom=0, top=10000)
+            with self.file1 as f:
+                self.lines = f.readlines()
+
+            self.data = [line.strip().split() for line in self.lines]
+            self.xdata, self.ydata = zip(*[(float(x), float(y)) for x, y in self.data])
+            self.ax.clear()
             self.ax.set_xlabel('Время, с')
             self.ax.set_ylabel('Импульсы')
-            self.ax.plot(self.t,
-            int(self.spinbox_var1.get()) + abs(int(self.spinbox_var1.get()) - int(self.spinbox_var2.get())) * 0.5 * (1 + np.sin(self.t / self.mnojtel)),
-                         '-rh', linewidth=1, markersize=1, markerfacecolor='b')
             self.ax.grid(color='black', linewidth=1.0)
-            self.canvas = FigureCanvasTkAgg(self.fig, master=self.f_graf)
-            self.canvas.draw()
-            self.canvas.get_tk_widget().grid(column=0, row=0)
-            Interface.window.after(100, None)
+            self.ax.set_xlim(min(self.xdata), max(self.xdata))
+            self.ax.set_ylim(min(min(self.ydata), int(self.spinbox_var1.get())),
+                                 max(max(self.ydata), int(self.spinbox_var2.get())))
+            self.line, = self.ax.plot([], [], 'ro-')
+
+            def init1():
+                self.line.set_data([], [])
+                return self.line,
 
+            def update1(frame):
+                self.line.set_data(self.xdata[:frame], self.ydata[:frame])
+                return self.line,
+
+            self.ani = FuncAnimation(self.fig, update1, frames=len(self.xdata),
+                                     init_func=init1, blit=True, interval=5, repeat=False)
+
+            self.canvas.draw()
+            print(self.ydata)
+            Interface.window.after(10, None)
 
 
     def mennuB(self):

Разница между файлами не показана из-за своего большого размера
+ 1000 - 0
coordinates.txt


Разница между файлами не показана из-за своего большого размера
+ 1000 - 0
coordinates1.txt


+ 0 - 0
generatorT.py