plotting.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Stephen Stengel <stephen.stengel@cwu.edu> 40819903
  5. #
  6. # Plotting script.
  7. import matplotlib.pyplot as plt
  8. from mpl_toolkits.mplot3d import Axes3D
  9. import numpy
  10. import os
  11. def main(args):
  12. myStuff = []
  13. for i in range(0, 20):
  14. myStuff.append( [i, i*2, i*3] )
  15. filename = "testfile.txt"
  16. printListListToFile(myStuff, filename)
  17. data = readListListFromFile(filename)
  18. # ~ data = readListListFromFile("real-test-tupples")
  19. print(data)
  20. #split into x y z arrays.
  21. X, Y, Z = convertToThreeArrays(data)
  22. picOutName = "graph-test"
  23. printGraph(X, Y, Z, "A TITLE lol", "xLable", "yLable", "ZLable", picOutName)
  24. os.system("sync")
  25. os.system("optipng ../pics/*.png")
  26. return 0
  27. #Name remains a list
  28. def convertToFiveArrays(data):
  29. M, N, T, TIME, NAME = map(list, zip(*data))
  30. M = numpy.array(M, dtype=numpy.float32)
  31. N = numpy.array(N, dtype=numpy.float32)
  32. T = numpy.array(T, dtype=numpy.float32)
  33. TIME = numpy.array(TIME, dtype=numpy.float32)
  34. return M, N, T, TIME, NAME
  35. #Prints a list of tupples to file.
  36. def printListListToFile(myTupples, filename):
  37. with open( "./data/" + filename, "w") as myFile:
  38. for thingy in myTupples:
  39. for i in range(0, len(thingy)):
  40. myFile.write(f"{thingy[i]}")
  41. if (i + 1) != len(thingy):
  42. myFile.write(" ")
  43. else:
  44. myFile.write("\n")
  45. def readListListFromFile(filename):
  46. outList = []
  47. with open(filename, "r") as myFile:
  48. for line in myFile:
  49. thisList = line.rstrip().split(" ")
  50. outList.append(thisList)
  51. return outList
  52. def printGraph(xArray, yArray, zArray, title, xLab, yLab, zLab, filename):
  53. myFigure = plt.figure()
  54. myFrame = myFigure.add_subplot(111, projection='3d')
  55. myFrame.scatter(xArray, yArray, zArray, marker = "o")
  56. # ~ myFrame.plot_trisurf(xArray, yArray, zArray)
  57. # ~ myFrame.scatter(xArray, yArray, zArray, marker = "o", label = "TEST LABEL LOL")
  58. # ~ myFrame.scatter(xArrayAho, yArrayAho, zArrayAho, marker = "^", label = "Aho Corasick")
  59. myFrame.set_title(title)
  60. myFrame.set_xlabel(xLab)
  61. myFrame.set_ylabel(yLab)
  62. myFrame.set_zlabel(zLab)
  63. # ~ myFrame.axes.set_xlim3d(left=0, right=200)
  64. # ~ myFrame.legend()
  65. # ~ myFrame.legend(loc = 6, ncol = 1)
  66. plt.savefig(filename + ".png")
  67. # ~ plt.show()#
  68. plt.clf()
  69. if __name__ == '__main__':
  70. import sys
  71. sys.exit(main(sys.argv))