Я пытаюсь отобразить результаты каждой итерации алгоритма (точки данных получаются довольно быстро).Я уже рассмотрел matplotlib.animation
и несколько решений, использующих другие пакеты, но ни один из них не использует подход ООП.
Я придумал этот код:
import matplotlib.animation as animation
import random
class LivePlot:
def __init__(self, title, x_label, y_label):
self.x = []
self.y = []
self.fig = plt.figure()
self.ax = self.fig.add_subplot(1, 1, 1)
# Create a blank line (will be updated in animate)
self.line, = self.ax.plot([], [])
# Add labels
plt.xlabel(x_label)
plt.ylabel(y_label)
plt.title(title)
self.anim = animation.FuncAnimation(self.fig, self.animate,
init_func=self.__init,
blit=True)
def __init(self):
self.line.set_data([], [])
return self.line,
def animate(self, i):
# Update line with new Y values
self.line.set_data(self.x, self.y)
return self.line,
# Set up plot to call animate() function periodically
def plot(self, xs, ys):
self.x.append(xs)
self.y.append(ys)
plt.show()
if __name__ == '__main__':
pl = LivePlot(title='test', x_label='iter', y_label='score')
for x in range(100):
y = random.randint(1,4)
pl.plot(x, y)
Это неЭто не работает, окно графика исчезает почти сразу, без вывода каких-либо данных.