This question is different from other animation questions as I'm trying to animate an alternating amounts of lines between points. For example, it may be between 3 points or 50 points.
Using the data frame below, the points are labelled in Item. The first two time stamps contain 4 points but this drops to 3 points for the final two time stamps. I'm trying to find an efficient way to combine all the potential lines act each time stamp into a single call function to animate.
The issue I'm having is I'm plotting each line manually. Therefore, the line between each point is currently hard-coded, which doesn't account for a change in the amount of lines.
I need something that combines the available lines first and then passes this to the animation.
For instance, A, B, C, D are currently labelled points in the first two time points. But this drops to A, B, C for the last two time points.
This following doesn't account for alternating amounts of lines.
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
import pandas as pd
df1 = pd.DataFrame({
'Time' : [1,1,1,1,2,2,2,2,3,3,3,4,4,4],
'Item' : ['A', 'B', 'C', 'D','A', 'B', 'C', 'D', 'A', 'B', 'C', 'A', 'B', 'C'],
'GroupA_X' : [3, 4, 5, 1, 2, 5, 6, 2, 1, 6, 7, 2, 7, 8],
'GroupA_Y' : [2, 4, 5, 1, 2, 5, 5, 2, 2, 6, 5, 1, 5, 4],
})
GA_X = np.array(df.groupby('Time')['GroupA_X'].apply(list).tolist())
GA_Y = np.array(df.groupby('Time')['GroupA_Y'].apply(list).tolist())
fig, ax = plt.subplots(figsize = (6,6))
ax.grid(False)
ax.set_xlim(0,10)
ax.set_ylim(0,10)
data = np.stack([GA_X, GA_Y], axis = 2)
vector1 = ax.annotate('', xy = data[0][0],
xytext = data[0][1],
arrowprops={'arrowstyle': "-", 'color': 'black'},
ha='center')
vector2 = ax.annotate('', xy = data[0][0],
xytext = data[0][2],
arrowprops={'arrowstyle': "-", 'color': 'black'},
ha='center')
vector3 = ax.annotate('', xy = data[0][1],
xytext = data[0][2],
arrowprops={'arrowstyle': "-", 'color': 'black'},
ha='center')
def animate(i):
start1 = np.r_[data[i, 0]]
end1 = np.r_[data[i, 1]]
vector1.set_position(start1)
vector1.xy = end1
start2 = np.r_[data[i, 0]]
end2 = np.r_[data[i, 2]]
vector2.set_position(start2)
vector2.xy = end2
start3 = np.r_[data[i, 1]]
end3 = np.r_[data[i, 2]]
vector3.set_position(start3)
vector3.xy = end3
return
ani = animation.FuncAnimation(fig, animate, interval = 100, blit = False)
Out:
data = np.stack([GA_X, GA_Y], axis = 2)
axis = normalize_axis_index(axis, result_ndim)
AxisError: axis 2 is out of bounds for array of dimension 2