I'm working with Matplotlib in Python to plot two elements (one is a scatter and the other is an image made from a matrix) 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 hundreds of lines, and therefore, many skeletons. Then, the text file is made of a certain number of skeletons (number_of_lines/20 = total of skeletons), that generates a movement. Therefore, the text file represents a movement.
An example of the text file content:
1.3200000e+002 8.0000000e+001 6.8100000e+002 1.0000000e+000
1.6900000e+002 8.1000000e+001 6.6500000e+002 1.0000000e+000
1.4800000e+002 7.5000000e+001 6.5000000e+002 0.0000000e+000
1.5100000e+002 9.8000000e+001 6.9800000e+002 0.0000000e+000
1.3200000e+002 1.2000000e+002 6.7400000e+002 1.0000000e+000
1.6100000e+002 1.2200000e+002 6.6200000e+002 8.1730800e-001
At each skeleton plotted, a distance matrix from each joint to all the other 19 joints is generated, and imgshow is used to transform the matrix in an image. Also, in the plotting of the skeletons, only two coordinates are taken, (x,y) to generate a 2D skeleton, so the third coordinate is ignored. I had to use scatter to plot the skeleton, since the joints are plotted first, and then the lines connecting the joints are plotted, in the second for loop of the code.
- The problem: I'm using grispdec to plot each skeleton, and imgshow to transform the distance matrix in an image. Those plots are inside a for loop, and I've adjusted their position separately, to "force" them to be shown side by side in the same canvas. As I've explained, there are many skeletons/matrix's image pairs in each text file, and I must associate a key press event with the plotting of each skeleton/matrix' image pair. It mustn't be necessary to close the canvas to see the next plot pair from the for loop.
Each canvas is made of two plots: the skeleton and it's distance matrix image.
The code:
from matplotlib import gridspec
import numpy as np
import matplotlib.pyplot as plt
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)
list_for_matrix = []
for i in range(number_of_postures):
list_for_matrix.append(movement[(i*20):((i+1)*20),:3])
matrixCoord = np.array(list_for_matrix)
matrixDistance= np.zeros((number_of_postures,20,20))
for k in range(number_of_postures):
for i in range(len(matrixDistance[0])):
for j in range(len(matrixDistance[0])):
matrixDistance[k,i,j] = np.linalg.norm(matrixCoord[k,i,:] - matrixCoord[k,j,:])
z = 0
for i in range(number_of_postures):
# first plot using GridSpec
#skeleton joints are plotted first:
gs = gridspec.GridSpec(1, 2, width_ratios=[3, 1])skeleton.
skeleton = movement[z:z+20, :2]
x = skeleton[:,0]
y = - skeleton[:,1]
gs.update(left=0.32, right=0.50, wspace=0.05)
plt.subplot(gs[-1,0])
plt.scatter(x,y, s=40)
plt.title('Skeleton')
z+=20
#plotting the lines that connect the joints in the first plot
for bone in bone_list:
plt.plot([x[bone[0]], x[bone[1]]], [y[bone[0]], y[bone[1]]], 'r')
#second plot: distance matrix image using imgshow
plt.subplot(233)
img = matrixDistance[i]
imgplot = plt.imshow(img)
imgplot.set_cmap('PuBu')
plt.colorbar()
plt.title('Distance Matrix Image')
plt.show()
My Question:
Is there a way to connect those two plotting methods to the canvas so that each skeletons/matrix's image pair (plot) will be shown only after a key press event, in the way the plots are constructed, with no altering in their structure ? I need to make a call of a canvas connection function as in
def update(event):
if event.key == "some_key":
#plot the skeleton and the matrix image i in the loop
fig.canvas.mpl_connect("key_press_event", update)
and the problem is that neither gridscpec or imgshow recognize:
canvas.mpl_connect
Another problem is that I don't know how to call a similar function as the example inside the for loop, so that each time the key is pressed, the function is called and the plotting will happen only afterwards, or somthing similar.