-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrafica.py
More file actions
54 lines (38 loc) · 1.62 KB
/
grafica.py
File metadata and controls
54 lines (38 loc) · 1.62 KB
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
from time import sleep
import numpy as np
import matplotlib.pyplot as plt
class Grafica:
def __init__(self):
self.fig, self.ax = plt.subplots(figsize=(10, 3), dpi=100)
self.line, = self.ax.plot([], [])
self.ax.set_xlim(0, 100) # Inicializar con un límite de 200
self.ax.set_ylim(-5, 5)
self.x_values = []
self.y_values = []
# Función para actualizar la gráfica con un nuevo valor
def update_plot(self, y):
if len(self.line.get_xdata()) == 0:
self.x_values = np.array([0])
else:
self.x_values = np.append(self.line.get_xdata(), self.line.get_xdata()[-1] + 1)
self.y_values = np.append(self.line.get_ydata(), y)
self.line.set_data( self.x_values, self.y_values)
# Ajustar el límite del eje x para mantener un intervalo de 100
if self.x_values[-1] > 100:
self.ax.set_xlim( self.x_values[-1] - 100, self.x_values[-1])
else:
self.ax.set_xlim(0, 100)
def update_plot2(self, x, y):
# Agregar el nuevo punto (x, y)
self.x_values = np.append(self.line.get_xdata(), x)
self.y_values = np.append(self.line.get_ydata(), y)
self.line.set_data(self.x_values, self.y_values)
# Ajustar el límite del eje x para mantener un intervalo de 100
if self.x_values[-1] > 100:
self.ax.set_xlim(self.x_values[-1] - 100, self.x_values[-1])
else:
self.ax.set_xlim(0, 100)
def detener_grafica(self):
# Detener la ejecución para ver la gráfica
plt.ioff()
plt.show()