source: sasview/src/sas/qtgui/Plotter.py @ 31c5b58

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 31c5b58 was 31c5b58, checked in by Piotr Rozyczko <rozyczko@…>, 7 years ago

Refactored to allow running with run.py.
Minor fixes to plotting.

  • Property mode set to 100644
File size: 3.3 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 sas.qtgui.PlotHelper as 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    @property
54    def data(self):
55        """ data getter """
56        return self._data
57
58    @data.setter
59    def data(self, value):
60        """ data setter """
61        self._data = value
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
86    def plot(self, marker=None, linestyle=None):
87        """
88        Plot self._data
89        """
90        # create an axis
91        ax = self._ax
92
93        if marker == None:
94            marker = '*'
95
96        if linestyle == None:
97            linestyle = '-'
98
99        # plot data with legend
100        ax.plot(self._data.x, self._data.y, marker=marker, linestyle=linestyle, label=self._title)
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
Note: See TracBrowser for help on using the repository browser.