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