0

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()
2
  • What exactly is the problem? Commented Jul 23, 2018 at 20:08
  • Do you want to learn something or just find someone to do your task? In the former case, split your task into small parts, and see where you get stuck. In this case, find out how to let something happen upon pressing a key in matplotlib (I suppose this can be googled literally). Next, find out how to change your code to implement what you found. If then there is a problem, try to solve it, or ask here. Commented Jul 24, 2018 at 17:21

1 Answer 1

1

well if you are using windows you could try using python windows API to detect a key press.

pip install pypiwin32

import win32api

def keypress(key):
    state = win32api.GetAsyncKeyState(ord(key))
    if(state < 0 or state == 1):
        return True
Sign up to request clarification or add additional context in comments.

1 Comment

Actually I'm using Linux, Ubuntu 16.04. Is there an equivalent API to Linux-Ubuntu?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.