plot.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. from matplotlib import pyplot as plt
  2. import numpy as np
  3. def plot_data_from(filename):
  4. data = []
  5. with open(filename) as f:
  6. lines = f.read().splitlines()
  7. for line in lines:
  8. positions = []
  9. for p in line.split(";")[:-1]:
  10. positions.append(list(map(float, p.split(", "))))
  11. data.append(positions)
  12. number_of_joints = len(data[0])
  13. x = [[] for i in range(number_of_joints)]
  14. y = [[] for i in range(number_of_joints)]
  15. z = [[] for i in range(number_of_joints)]
  16. dx = [[] for i in range(number_of_joints)]
  17. dy = [[] for i in range(number_of_joints)]
  18. dz = [[] for i in range(number_of_joints)]
  19. for time in range(len(data)):
  20. for joint in range(len(data[time])):
  21. x[joint].append(data[time][joint][0])
  22. y[joint].append(data[time][joint][1])
  23. z[joint].append(data[time][joint][2])
  24. if (time > 0):
  25. dx[joint].append(x[joint][-1] - x[joint][-2])
  26. dy[joint].append(y[joint][-1] - y[joint][-2])
  27. dz[joint].append(z[joint][-1] - z[joint][-2])
  28. labels = ["nose","leftShoulder","rightShoulder","leftElbow","rightElbow","leftWrist","rightWrist","leftHip","rightHip","leftKnee","rightKnee","leftAnkle","rightAnkle"]
  29. NUM_COLORS = len(labels)
  30. cm = plt.get_cmap('tab20')
  31. fig = plt.figure("x")
  32. ax = fig.add_subplot(111)
  33. ax.set_prop_cycle(color=[cm(1.*i/NUM_COLORS) for i in range(NUM_COLORS)])
  34. for time, v in enumerate(x):
  35. plt.plot(v, label=labels[time])
  36. plt.legend(loc="center left")
  37. fig = plt.figure("y")
  38. ax = fig.add_subplot(111)
  39. ax.set_prop_cycle(color=[cm(1.*i/NUM_COLORS) for i in range(NUM_COLORS)])
  40. for time, v in enumerate(y):
  41. plt.plot(v, label=labels[time])
  42. plt.legend(loc="center left")
  43. fig = plt.figure("z")
  44. ax = fig.add_subplot(111)
  45. ax.set_prop_cycle(color=[cm(1.*i/NUM_COLORS) for i in range(NUM_COLORS)])
  46. for time, v in enumerate(z):
  47. plt.plot(v, label=labels[time])
  48. plt.legend(loc="center left")
  49. fig = plt.figure("dx")
  50. ax = fig.add_subplot(111)
  51. ax.set_prop_cycle(color=[cm(1.*i/NUM_COLORS) for i in range(NUM_COLORS)])
  52. for time, v in enumerate(dx):
  53. plt.plot(v, label=labels[time])
  54. plt.legend(loc="center left")
  55. fig = plt.figure("dy")
  56. ax = fig.add_subplot(111)
  57. ax.set_prop_cycle(color=[cm(1.*i/NUM_COLORS) for i in range(NUM_COLORS)])
  58. for time, v in enumerate(dy):
  59. plt.plot(v, label=labels[time])
  60. plt.legend(loc="center left")
  61. fig = plt.figure("dz")
  62. ax = fig.add_subplot(111)
  63. ax.set_prop_cycle(color=[cm(1.*i/NUM_COLORS) for i in range(NUM_COLORS)])
  64. for time, v in enumerate(dz):
  65. plt.plot(v, label=labels[time])
  66. plt.legend(loc="center left")
  67. plt.show()
  68. plot_data_from("meanFilteredData.csv")