MatPlotLibWrapper.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. '''
  2. Created on May 15, 2017
  3. @author: Bart
  4. '''
  5. import matplotlib.pyplot as plt
  6. import json
  7. from plot import Color, LineStyle
  8. class MatPlotLibWrapper:
  9. def visualize(self, plots):
  10. i = 1
  11. for plot in plots:
  12. plot = json.loads(plot)
  13. # create a new subplot
  14. plt.subplot(len(plots), 1, i)
  15. # do all datasets for this subplot
  16. for dataset in plot['datasets']:
  17. stylecode = ""
  18. if dataset['color'] != None or dataset['linestyle'] != None:
  19. stylecode = MatPlotLibWrapper.__color_code(dataset['color'])+MatPlotLibWrapper.__linestyle_code(dataset['linestyle'])
  20. # plot data with code and style
  21. plt.plot([p['x'] for p in dataset['datapoints']],
  22. [p['y'] for p in dataset['datapoints']],
  23. stylecode)
  24. # set title
  25. if plot['title'] != None:
  26. plt.title(plot['title'])
  27. # set x and y axis labels
  28. if plot['x']['unit']:
  29. plt.xlabel("%s (%s)" % (plot['x']['name'], plot['x']['unit']))
  30. else:
  31. plt.xlabel("%s" % plot['x']['name'])
  32. if plot['y']['unit']:
  33. plt.ylabel("%s (%s)" % (plot['y']['name'], plot['y']['unit']))
  34. else:
  35. plt.ylabel("%s" % plot['y']['name'])
  36. # make legend
  37. if plot['legend']:
  38. plt.legend([ds['legend'] for ds in plot['datasets']])
  39. # set axis ranges
  40. if plot['x']['lim_low'] != None and plot['x']['lim_high'] != None:
  41. plt.xlim([plot['x']['lim_low'], plot['x']['lim_high']])
  42. if plot['y']['lim_low'] != None and plot['y']['lim_high'] != None:
  43. plt.ylim([plot['y']['lim_low'], plot['y']['lim_high']])
  44. i += 1
  45. plt.show()
  46. @staticmethod
  47. def __color_code(color):
  48. if color == Color.BLUE:
  49. return "b"
  50. elif color == Color.GREEN:
  51. return "g"
  52. elif color == Color.RED:
  53. return "r"
  54. elif color == Color.CYAN:
  55. return "c"
  56. elif color == Color.MAGENTA:
  57. return "m"
  58. elif color == Color.YELLOW:
  59. return "y"
  60. elif color == Color.BLACK:
  61. return "k"
  62. elif color == Color.WHITE:
  63. return "w"
  64. else:
  65. return MatPlotLibWrapper.__color_code(Color.BLUE)
  66. @staticmethod
  67. def __linestyle_code(linestyle):
  68. if linestyle == LineStyle.SOLID:
  69. return "-"
  70. elif linestyle == LineStyle.DASHDOT:
  71. return "-."
  72. elif linestyle == LineStyle.DASHED:
  73. return "--"
  74. elif linestyle == LineStyle.DOTTED:
  75. return ":"
  76. elif linestyle == LineStyle.PIXEL:
  77. return ","
  78. elif linestyle == LineStyle.POINT:
  79. return "."
  80. elif linestyle == LineStyle.CIRCLE:
  81. return "o"
  82. elif linestyle == LineStyle.STAR:
  83. return "*"
  84. elif linestyle == LineStyle.PLUS:
  85. return "+"
  86. elif linestyle == LineStyle.X:
  87. return "x"
  88. else:
  89. return MatPlotLibWrapper.__linestyle_code(LineStyle.CIRCLE)