source: sasview/src/sas/qtgui/Plotter2D.py @ 14d9c7b

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

2D plotter cleanup

  • Property mode set to 100755
File size: 4.3 KB
Line 
1import logging
2import copy
3import numpy
4import pylab
5
6from PyQt4 import QtGui
7
8# TODO: Replace the qt4agg calls below with qt5 equivalent.
9# Requires some code modifications.
10# https://www.boxcontrol.net/embedding-matplotlib-plot-on-pyqt5-gui.html
11#
12from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
13from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
14import matplotlib.pyplot as plt
15
16DEFAULT_CMAP = pylab.cm.jet
17
18import PlotUtilities
19import PlotHelper
20
21class Plotter2D(QtGui.QDialog):
22    def __init__(self, parent=None):
23        super(Plotter2D, self).__init__(parent)
24
25        # Required for the communicator
26        self.parent = parent
27
28        # a figure instance to plot on
29        self.figure = plt.figure()
30
31        # this is the Canvas Widget that displays the `figure`
32        # it takes the `figure` instance as a parameter to __init__
33        self.canvas = FigureCanvas(self.figure)
34
35        # this is the Navigation widget
36        # it takes the Canvas widget and a parent
37        self.toolbar = NavigationToolbar(self.canvas, self)
38
39        # set the layout
40        layout = QtGui.QVBoxLayout()
41        layout.addWidget(self.canvas)
42        layout.addWidget(self.toolbar)
43        self.setLayout(layout)
44
45        # defaults
46        self._current_plot = 111
47        self._data = []
48        self._qx_data = []
49        self._qy_data = []
50
51        # default color map
52        self._cmap = DEFAULT_CMAP
53
54        self._ax = self.figure.add_subplot(self._current_plot)
55
56        # Notify the helper
57        PlotHelper.addPlot(self)
58        # Notify the listeners
59        self.parent.communicator.activeGraphsSignal.emit(PlotHelper.currentPlots())
60
61    def data(self, data=None):
62        """ data setter """
63        self._data = data
64        self._qx_data=data.qx_data
65        self._qy_data=data.qy_data
66        self._xmin=data.xmin
67        self._xmax=data.xmax
68        self._ymin=data.ymin
69        self._ymax=data.ymax
70        self._zmin=data.zmin
71        self._zmax=data.zmax
72        self._color=0
73        self._symbol=0
74        self._label=data.name
75        self._scale = 'linear'
76
77        self.x_label(xlabel=data._xaxis + data._xunit)
78        self.y_label(ylabel=data._yaxis + data._yunit)
79        self.title(title=data.title)
80
81    def title(self, title=""):
82        """ title setter """
83        self._title = title
84
85    def id(self, id=""):
86        """ id setter """
87        self._id = id
88
89    def x_label(self, xlabel=""):
90        """ x-label setter """
91        self._xlabel = r'$%s$'% xlabel
92
93    def y_label(self, ylabel=""):
94        """ y-label setter """
95        self._ylabel = r'$%s$'% ylabel
96
97    def clean(self):
98        """
99        Redraw the graph
100        """
101        self.figure.delaxes(self._ax)
102        self._ax = self.figure.add_subplot(self._current_plot)
103
104    def plot(self, marker=None, linestyle=None):
105        """
106        Plot 2D self._data
107        """
108        # create an axis
109        ax = self._ax
110
111        # graph properties
112        ax.set_xlabel(self._xlabel)
113        ax.set_ylabel(self._ylabel)
114        ax.set_title(label=self._title)
115
116        # Re-adjust colorbar
117        # self.figure.subplots_adjust(left=0.2, right=.8, bottom=.2)
118
119        output = PlotUtilities.build_matrix(self._data.data, self._qx_data, self._qy_data)
120
121        im = ax.imshow(output,
122                       interpolation='nearest',
123                       origin='lower',
124                       vmin=self._zmin, vmax=self._zmax,
125                       cmap=self._cmap,
126                       extent=(self._xmin, self._xmax,
127                               self._ymin, self._ymax))
128
129        cbax = self.figure.add_axes([0.84, 0.2, 0.02, 0.7])
130        cb = self.figure.colorbar(im, cax=cbax)
131        cb.update_bruteforce(im)
132        cb.set_label('$' + self._scale + '$')
133
134        # Schedule the draw for the next time the event loop is idle.
135        self.canvas.draw_idle()
136
137    def closeEvent(self, event):
138        """
139        Overwrite the close event adding helper notification
140        """
141        # Please remove me from your database.
142        PlotHelper.deletePlot(PlotHelper.idOfPlot(self))
143        # Notify the listeners
144        self.parent.communicator.activeGraphsSignal.emit(PlotHelper.currentPlots())
145        event.accept()
146
Note: See TracBrowser for help on using the repository browser.