I am trying to update a line drawn between two plots with the FuncAnimation function from matplotlib. I saw in other questions how to update the values in the ConnectionPatch, but the problem is, that the line is only updatet in the subplot it is added as artist to. Here I coded a small example to reproduce my problem:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import ConnectionPatch
from matplotlib.animation import FuncAnimation
data = np.array([[1, 2, 3, 3, 3, 3, 1, 1, 2, -1, -1, 5, 1, 2, 0],
[1, 2, 3, 3, 3, 3, 1, 1, 2, -1, -1, 5, 1, 2, 0]])
# create plot
fig, ax = plt.subplots(2,1,sharex=True)
sub_plot1 = ax[0].plot(data[0, :])
sub_plot2 = ax[1].plot(data[1, :], color='yellow')
# line between plots
con = ConnectionPatch(xyA=(0, 4), xyB=(0, 0), coordsA="data", coordsB="data", axesA=ax[0], axesB=ax[1], color="red")
ax[1].add_artist(con)
#fig.add_artist(con)
x = 0
# animate Function for FuncAnimation
def animate(i):
global x
x += 1
if x > 14:
x = 0
con.xy1 = (x, 4)
con.xy2 = (x, 0)
return con,
myAnimation = FuncAnimation(fig, animate, interval=1000, blit=True)
plt.show()