Advertisement
Guest User

Untitled

a guest
Apr 28th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. import math
  2.  
  3. from PyQt4 import QtCore, QtGui
  4.  
  5. from matplotlib.figure import Figure
  6. from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
  7. from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
  8.  
  9.  
  10. class MyFigure(Figure, QtCore.QThread):
  11. def __init__(self, parent, *args, **kwargs):
  12. QtCore.QThread.__init__(self, parent)
  13. Figure.__init__(self, *args, **kwargs)
  14.  
  15. self.plot_data = list()
  16.  
  17. def start_plotting_thread(self, plot_data, on_finish=None):
  18. """ Start plotting """
  19. self.plot_data = plot_data
  20.  
  21. if on_finish is not None:
  22. self.finished.connect(on_finish)
  23.  
  24. self.start()
  25.  
  26. def run(self):
  27. """ Run as a thread """
  28. # Figure out rows and columns
  29. total_plots = len(self.plot_data)
  30.  
  31. columns = int(math.sqrt(total_plots))
  32. if columns < 1:
  33. columns = 1
  34.  
  35. rows = int(total_plots / columns)
  36. if (total_plots % columns) > 0:
  37. rows += 1
  38. if rows < 1:
  39. rows = 1
  40.  
  41. # Plot Data
  42. for plot_index, _plot_data in enumerate(self.plot_data):
  43. plot_number = plot_index + 1
  44. args = (rows, columns, plot_number)
  45. kwargs = {
  46. 'title': _plot_data['title'],
  47. 'xlabel': _plot_data['xlabel'],
  48. 'ylabel': _plot_data['ylabel']
  49. }
  50.  
  51. figure = self.add_subplot(*args, **kwargs)
  52.  
  53. figure.plot(_plot_data['data'])
  54.  
  55.  
  56. class PlotDialog(QtGui.QDialog):
  57. def __init__(self, parent):
  58. super(PlotDialog, self).__init__(parent, QtCore.Qt.WindowMinMaxButtonsHint | QtCore.Qt.WindowCloseButtonHint)
  59.  
  60. self.figure = MyFigure(self)
  61. self.canvas = FigureCanvas(self.figure)
  62. self.toolbar = NavigationToolbar(self.canvas, self)
  63.  
  64. self.layout = QtGui.QGridLayout()
  65. self.setLayout(self.layout)
  66.  
  67. layout = [
  68. [self.canvas],
  69. [self.toolbar],
  70. ]
  71.  
  72. for row_index, columns in enumerate(layout):
  73. if type(columns) is list:
  74. for column_index, widget in enumerate(columns):
  75. if widget is not None:
  76. self.layout.addWidget(widget, row_index, column_index)
  77.  
  78. def draw_plots(self, plot_data):
  79. """ Plot Plots """
  80. self.figure.start_plotting_thread(plot_data, on_finish=self.finish_drawing_plots)
  81.  
  82. def finish_drawing_plots(self):
  83. """ Finish drawing plots """
  84. self.canvas.draw()
  85. self.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement