[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 | |
---|
| 13 | import PlotHelper |
---|
| 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 | |
---|
| 53 | def data(self, data=None): |
---|
| 54 | """ data setter """ |
---|
| 55 | self._data = data |
---|
| 56 | |
---|
| 57 | def title(self, title=""): |
---|
| 58 | """ title setter """ |
---|
| 59 | self._title = title |
---|
| 60 | |
---|
| 61 | def id(self, id=""): |
---|
| 62 | """ id setter """ |
---|
| 63 | self._id = id |
---|
| 64 | |
---|
| 65 | def x_label(self, xlabel=""): |
---|
| 66 | """ x-label setter """ |
---|
| 67 | self._xlabel = xlabel |
---|
| 68 | |
---|
| 69 | def y_label(self, ylabel=""): |
---|
| 70 | """ y-label setter """ |
---|
| 71 | self._ylabel = ylabel |
---|
| 72 | |
---|
| 73 | def clean(self): |
---|
| 74 | """ |
---|
| 75 | Redraw the graph |
---|
| 76 | """ |
---|
| 77 | self.figure.delaxes(self._ax) |
---|
| 78 | self._ax = self.figure.add_subplot(self._current_plot) |
---|
| 79 | |
---|
[39551a68] | 80 | def plot(self, marker=None, linestyle=None): |
---|
[8cb6cd6] | 81 | """ |
---|
| 82 | Plot self._data |
---|
| 83 | """ |
---|
| 84 | # create an axis |
---|
| 85 | ax = self._ax |
---|
| 86 | |
---|
[39551a68] | 87 | if marker == None: |
---|
| 88 | marker = '*' |
---|
| 89 | |
---|
| 90 | if linestyle == None: |
---|
| 91 | linestyle = '-' |
---|
| 92 | |
---|
[8cb6cd6] | 93 | # plot data with legend |
---|
[39551a68] | 94 | ax.plot(self._data.x, self._data.y, marker=marker, linestyle=linestyle, label=self._title) |
---|
[8cb6cd6] | 95 | |
---|
| 96 | # Now add the legend with some customizations. |
---|
| 97 | legend = ax.legend(loc='lower left', shadow=True) |
---|
| 98 | |
---|
| 99 | ax.set_ylabel(self._ylabel) |
---|
| 100 | ax.set_xlabel(self._xlabel) |
---|
| 101 | |
---|
| 102 | ax.set_yscale('log') |
---|
| 103 | ax.set_xscale('log') |
---|
| 104 | |
---|
| 105 | # refresh canvas |
---|
| 106 | self.canvas.draw() |
---|
| 107 | |
---|
| 108 | def closeEvent(self, event): |
---|
| 109 | """ |
---|
| 110 | Overwrite the close event adding helper notification |
---|
| 111 | """ |
---|
| 112 | # Please remove me from your database. |
---|
| 113 | PlotHelper.deletePlot(PlotHelper.idOfPlot(self)) |
---|
| 114 | # Notify the listeners |
---|
| 115 | self.parent.communicator.activeGraphsSignal.emit(PlotHelper.currentPlots()) |
---|
| 116 | event.accept() |
---|
| 117 | |
---|