source: sasview/src/sas/qtgui/Plotter.py @ 4b71e91

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since 4b71e91 was 8cb6cd6, checked in by Piotr Rozyczko <piotr.rozyczko@…>, 8 years ago

Plot handler prototype + append plot functionality

  • Property mode set to 100755
File size: 3.0 KB
Line 
1import logging
2
3from 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#
9from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
10from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
11import matplotlib.pyplot as plt
12
13import PlotHelper
14
15class 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
80    def plot(self):
81        """
82        Plot self._data
83        """
84        # create an axis
85        ax = self._ax
86
87        # plot data with legend
88        ax.plot(self._data.x, self._data.y, '*-', label=self._title)
89
90        # Now add the legend with some customizations.
91        legend = ax.legend(loc='lower left', shadow=True)
92
93        ax.set_ylabel(self._ylabel)
94        ax.set_xlabel(self._xlabel)
95
96        ax.set_yscale('log')
97        ax.set_xscale('log')
98
99        # refresh canvas
100        self.canvas.draw()
101
102    def closeEvent(self, event):
103        """
104        Overwrite the close event adding helper notification
105        """
106        # Please remove me from your database.
107        PlotHelper.deletePlot(PlotHelper.idOfPlot(self))
108        # Notify the listeners
109        self.parent.communicator.activeGraphsSignal.emit(PlotHelper.currentPlots())
110        event.accept()
111
Note: See TracBrowser for help on using the repository browser.