1 | from PyQt4 import QtGui |
---|
2 | |
---|
3 | # TODO: Replace the qt4agg calls below with qt5 equivalent. |
---|
4 | # Requires some code modifications. |
---|
5 | # https://www.boxcontrol.net/embedding-matplotlib-plot-on-pyqt5-gui.html |
---|
6 | # |
---|
7 | from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas |
---|
8 | from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar |
---|
9 | import matplotlib.pyplot as plt |
---|
10 | |
---|
11 | class Plotter(QtGui.QDialog): |
---|
12 | def __init__(self, parent=None): |
---|
13 | super(Plotter, self).__init__(parent) |
---|
14 | |
---|
15 | # a figure instance to plot on |
---|
16 | self.figure = plt.figure() |
---|
17 | |
---|
18 | # this is the Canvas Widget that displays the `figure` |
---|
19 | # it takes the `figure` instance as a parameter to __init__ |
---|
20 | self.canvas = FigureCanvas(self.figure) |
---|
21 | |
---|
22 | # this is the Navigation widget |
---|
23 | # it takes the Canvas widget and a parent |
---|
24 | self.toolbar = NavigationToolbar(self.canvas, self) |
---|
25 | |
---|
26 | # set the layout |
---|
27 | layout = QtGui.QVBoxLayout() |
---|
28 | layout.addWidget(self.canvas) |
---|
29 | layout.addWidget(self.toolbar) |
---|
30 | self.setLayout(layout) |
---|
31 | |
---|
32 | # defaults |
---|
33 | self._current_plot = 111 |
---|
34 | self._data = [] |
---|
35 | self._title = "Plot" |
---|
36 | self._id = "" |
---|
37 | self._xlabel = "X" |
---|
38 | self._ylabel = "Y" |
---|
39 | self._ax = self.figure.add_subplot(self._current_plot) |
---|
40 | |
---|
41 | |
---|
42 | def data(self, data=None): |
---|
43 | """ |
---|
44 | """ |
---|
45 | self._data = data |
---|
46 | |
---|
47 | def title(self, title=""): |
---|
48 | """ |
---|
49 | """ |
---|
50 | self._title = title |
---|
51 | |
---|
52 | def id(self, id=""): |
---|
53 | """ |
---|
54 | """ |
---|
55 | self._id = id |
---|
56 | |
---|
57 | def x_label(self, xlabel=""): |
---|
58 | """ |
---|
59 | """ |
---|
60 | self._xlabel = xlabel |
---|
61 | |
---|
62 | def y_label(self, ylabel=""): |
---|
63 | """ |
---|
64 | """ |
---|
65 | self._ylabel = ylabel |
---|
66 | |
---|
67 | def clean(self): |
---|
68 | """ |
---|
69 | """ |
---|
70 | self.figure.delaxes(self._ax) |
---|
71 | self._ax = self.figure.add_subplot(self._current_plot) |
---|
72 | |
---|
73 | def plot(self): |
---|
74 | """ |
---|
75 | plot self._data |
---|
76 | """ |
---|
77 | # create an axis |
---|
78 | ax = self._ax |
---|
79 | |
---|
80 | # plot data with legend |
---|
81 | ax.plot(self._data.x, self._data.y, '*-', label=self._title) |
---|
82 | |
---|
83 | # Now add the legend with some customizations. |
---|
84 | legend = ax.legend(loc='lower left', shadow=True) |
---|
85 | |
---|
86 | ax.set_ylabel(self._ylabel) |
---|
87 | ax.set_xlabel(self._xlabel) |
---|
88 | |
---|
89 | ax.set_yscale('log') |
---|
90 | ax.set_xscale('log') |
---|
91 | |
---|
92 | # refresh canvas |
---|
93 | self.canvas.draw() |
---|