I want to update data on a matplolib plot, thus I have patched code from several examples. Just for illustration purpose, I have two different matrix data which will be plotted in two different subplots alternately. This is the code:
import numpy as np
import matplotlib.pyplot as plt
import time
matrix1 = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
matrix2 = np.array([[2,3,4,5],[6,7,8,9],[10,11,12,13]])
plt.ion()
fig = plt.figure()
ax1 = fig.add_subplot(222)
ax2 = fig.add_subplot(221)
def update(mat1, mat2):
ax1.plot(mat1.T, 'ro', color='red')
ax2.plot(mat2.T, 'ro', color='blue')
fig.canvas.draw()
fig.canvas.flush_events()
time.sleep(1)
ax1.plot(mat2.T, 'ro', color='blue')
ax2.plot(mat1.T, 'ro', color='red')
fig.canvas.draw()
fig.canvas.flush_events()
time.sleep(1)
for i in range(10):
update(matrix1, matrix2)
which obviously doesn't work. As it is illustrated below, data of matrix1 (red) and matrix2 (blue) should appear not simultaneously in each subplot, but one matrix an each subplot and reverse position after each second.
Any suggestions would be appreciated.
P.S. I know that there are similar questions which I tried to consult, but with no luck.
