[8cb6cd6] | 1 | import logging |
---|
| 2 | |
---|
| 3 | from PyQt4 import QtGui |
---|
| 4 | |
---|
| 5 | # TODO: Replace the qt4agg calls below with qt5 equivalent. |
---|
| 6 | # Requires some code modifications. |
---|
| 7 | # https://www.boxcontrol.net/embedding-matplotlib-plot-on-pyqt5-gui.html |
---|
| 8 | # |
---|
| 9 | from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas |
---|
| 10 | from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar |
---|
| 11 | import matplotlib.pyplot as plt |
---|
| 12 | |
---|
[31c5b58] | 13 | import sas.qtgui.PlotHelper as PlotHelper |
---|
[8cb6cd6] | 14 | |
---|
| 15 | class Plotter(QtGui.QDialog): |
---|
| 16 | def __init__(self, parent=None): |
---|
| 17 | super(Plotter, self).__init__(parent) |
---|
| 18 | |
---|
| 19 | # Required for the communicator |
---|
| 20 | self.parent = parent |
---|
| 21 | |
---|
| 22 | # a figure instance to plot on |
---|
| 23 | self.figure = plt.figure() |
---|
| 24 | |
---|
| 25 | # this is the Canvas Widget that displays the `figure` |
---|
| 26 | # it takes the `figure` instance as a parameter to __init__ |
---|
| 27 | self.canvas = FigureCanvas(self.figure) |
---|
| 28 | |
---|
| 29 | # this is the Navigation widget |
---|
| 30 | # it takes the Canvas widget and a parent |
---|
| 31 | self.toolbar = NavigationToolbar(self.canvas, self) |
---|
| 32 | |
---|
| 33 | # set the layout |
---|
| 34 | layout = QtGui.QVBoxLayout() |
---|
| 35 | layout.addWidget(self.canvas) |
---|
| 36 | layout.addWidget(self.toolbar) |
---|
| 37 | self.setLayout(layout) |
---|
| 38 | |
---|
| 39 | # defaults |
---|
| 40 | self._current_plot = 111 |
---|
| 41 | self._data = [] |
---|
| 42 | self._title = "Plot" |
---|
| 43 | self._id = "" |
---|
| 44 | self._xlabel = "X" |
---|
| 45 | self._ylabel = "Y" |
---|
| 46 | self._ax = self.figure.add_subplot(self._current_plot) |
---|
| 47 | |
---|
| 48 | # Notify the helper |
---|
| 49 | PlotHelper.addPlot(self) |
---|
| 50 | # Notify the listeners |
---|
| 51 | self.parent.communicator.activeGraphsSignal.emit(PlotHelper.currentPlots()) |
---|
| 52 | |
---|
[31c5b58] | 53 | @property |
---|
| 54 | def data(self): |
---|
| 55 | """ data getter """ |
---|
| 56 | return self._data |
---|
| 57 | |
---|
| 58 | @data.setter |
---|
| 59 | def data(self, value): |
---|
[8cb6cd6] | 60 | """ data setter """ |
---|
[31c5b58] | 61 | self._data = value |
---|
[8cb6cd6] | 62 | |
---|
| 63 | def title(self, title=""): |
---|
| 64 | """ title setter """ |
---|
| 65 | self._title = title |
---|
| 66 | |
---|
| 67 | def id(self, id=""): |
---|
| 68 | """ id setter """ |
---|
| 69 | self._id = id |
---|
| 70 | |
---|
| 71 | def x_label(self, xlabel=""): |
---|
| 72 | """ x-label setter """ |
---|
| 73 | self._xlabel = xlabel |
---|
| 74 | |
---|
| 75 | def y_label(self, ylabel=""): |
---|
| 76 | """ y-label setter """ |
---|
| 77 | self._ylabel = ylabel |
---|
| 78 | |
---|
| 79 | def clean(self): |
---|
| 80 | """ |
---|
| 81 | Redraw the graph |
---|
| 82 | """ |
---|
| 83 | self.figure.delaxes(self._ax) |
---|
| 84 | self._ax = self.figure.add_subplot(self._current_plot) |
---|
| 85 | |
---|
[39551a68] | 86 | def plot(self, marker=None, linestyle=None): |
---|
[8cb6cd6] | 87 | """ |
---|
| 88 | Plot self._data |
---|
| 89 | """ |
---|
| 90 | # create an axis |
---|
| 91 | ax = self._ax |
---|
| 92 | |
---|
[39551a68] | 93 | if marker == None: |
---|
| 94 | marker = '*' |
---|
| 95 | |
---|
| 96 | if linestyle == None: |
---|
| 97 | linestyle = '-' |
---|
| 98 | |
---|
[8cb6cd6] | 99 | # plot data with legend |
---|
[39551a68] | 100 | ax.plot(self._data.x, self._data.y, marker=marker, linestyle=linestyle, label=self._title) |
---|
[8cb6cd6] | 101 | |
---|
| 102 | # Now add the legend with some customizations. |
---|
| 103 | legend = ax.legend(loc='lower left', shadow=True) |
---|
| 104 | |
---|
| 105 | ax.set_ylabel(self._ylabel) |
---|
| 106 | ax.set_xlabel(self._xlabel) |
---|
| 107 | |
---|
| 108 | ax.set_yscale('log') |
---|
| 109 | ax.set_xscale('log') |
---|
| 110 | |
---|
| 111 | # refresh canvas |
---|
| 112 | self.canvas.draw() |
---|
| 113 | |
---|
| 114 | def closeEvent(self, event): |
---|
| 115 | """ |
---|
| 116 | Overwrite the close event adding helper notification |
---|
| 117 | """ |
---|
| 118 | # Please remove me from your database. |
---|
| 119 | PlotHelper.deletePlot(PlotHelper.idOfPlot(self)) |
---|
| 120 | # Notify the listeners |
---|
| 121 | self.parent.communicator.activeGraphsSignal.emit(PlotHelper.currentPlots()) |
---|
| 122 | event.accept() |
---|
| 123 | |
---|