The code below represents the plotting of a graph that corresponds to the reading of a text file with n lines. Each line contains 4 columns,the first three columns are coordinates of (x,y,z) points, and the fourth column is a binary variable not necessary for this plotting.
At each 20 lines read, a skeleton is read, this skeleton being a group of 20 (x,y,z) points or joints, each joint made by the first three columns of each line.
The text file contains 860 lines, and 860/20 = 43, being 20 the number of joints to create a skeleton of (x,y,z) joints. Then, the text file is made of 43 skeletons, that generates a movement. Therefore, the text file represents a movement.
In the plotting of the skeletons, only two coordinates are taken, (x,y) to generate a 2D skeleton, so the third coordinate is ignored. And, the plotting is already inside a loop, but the key pressing event part is not implemented yet, the next skeleton is plotted when the current canvas is closed.
I've posted two pictures below of two consecutive canvas. The canvas ONE has to be closed before the canvas TWO is plotted. I need to associate that code to a key press event, such as: if the right arrow key is pressed, for example, then the next skeleton is plotted, otherwise, the next skeleton is never shown, but it mustn't be necessary to close a canvas to plot the next.
First plot canvas: https://i.sstatic.net/XxsNZ.png
Consecutive plot's canvas, shown after closing the first: https://i.sstatic.net/a1P8L.png
The code:
import matplotlib.pyplot as plt
import numpy as np
movement = np.loadtxt("file01.txt")
bone_list = [[1, 3], [2, 3], [3, 4], [4, 7], [5, 7], [6, 7], [1, 8], [2, 9], [8, 10], [9, 11], [10, 12], [11, 13], [5, 14], [6, 15], [14, 16], [15, 17], [16, 18], [17, 19], [3, 20]]
bone_list = np.array(bone_list) - 1
number_of_postures = int(len(movement)/20)
for i in range(number_of_postures):
fig, ax = plt.subplots(1, figsize=(3, 8))
plt.title('Skeleton')
plt.xlim(100, 180)
plt.ylim(-250, 0)
skeleton = movement[i*20:(i+1)*20]
x = skeleton[:, 0]
y = -skeleton[:, 1]
sc = ax.scatter(x, y, s=40)
for bone in bone_list:
ax.plot([x[bone[0]], x[bone[1]]], [y[bone[0]], y[bone[1]]], 'r')
plt.show()