source: sasview/src/sas/qtgui/Plotter2D.py @ fecfe28

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

Code review for 3D plots.

  • Property mode set to 100644
File size: 7.6 KB
Line 
1import copy
2import numpy
3import pylab
4
5from PyQt4 import QtGui
6
7DEFAULT_CMAP = pylab.cm.jet
8
9import sas.qtgui.PlotUtilities as PlotUtilities
10from sas.qtgui.PlotterBase import PlotterBase
11from mpl_toolkits.mplot3d import Axes3D
12
13# Minimum value of Z for which we will present data.
14MIN_Z=-32
15
16class Plotter2DWidget(PlotterBase):
17    """
18    2D Plot widget for use with a QDialog
19    """
20    def __init__(self, parent=None, manager=None, quickplot=False, dimension=2):
21        self.dimension = dimension
22        super(Plotter2DWidget, self).__init__(parent, manager=manager, quickplot=quickplot)
23
24    @property
25    def data(self):
26        return self._data
27
28    @data.setter
29    def data(self, data=None):
30        """ data setter """
31        self._data = data
32        self.qx_data = data.qx_data
33        self.qy_data = data.qy_data
34        self.xmin = data.xmin
35        self.xmax = data.xmax
36        self.ymin = data.ymin
37        self.ymax = data.ymax
38        self.zmin = data.zmin
39        self.zmax = data.zmax
40        self.label = data.name
41        self.xLabel = "%s(%s)"%(data._xaxis, data._xunit)
42        self.yLabel = "%s(%s)"%(data._yaxis, data._yunit)
43        self.title(title=data.title)
44
45    def plot(self, marker=None, linestyle=None):
46        """
47        Plot 2D self._data
48        """
49        # Toggle the scale
50        zmin_2D_temp = self.zmin
51        zmax_2D_temp = self.zmax
52        # self.scale predefined in the baseclass
53        if self.scale == 'log_{10}':
54            self.scale = 'linear'
55            if not self.zmin is None:
56                zmin_2D_temp = numpy.power(10, self.zmin)
57            if not self.zmax is None:
58                zmax_2D_temp = numpy.power(10, self.zmax)
59        else:
60            self.scale = 'log_{10}'
61            if not self.zmin is None:
62                # min log value: no log(negative)
63                if self.zmin <= 0:
64                    zmin_2D_temp = MIN_Z
65                else:
66                    zmin_2D_temp = numpy.log10(self.zmin)
67            if not self.zmax is None:
68                zmax_2D_temp = numpy.log10(self.zmax)
69
70        # Prepare and show the plot
71        self.showPlot(data=self.data.data,
72                      qx_data=self.qx_data,
73                      qy_data=self.qy_data,
74                      xmin=self.xmin,
75                      xmax=self.xmax,
76                      ymin=self.ymin, ymax=self.ymax,
77                      cmap=self.cmap, zmin=zmin_2D_temp,
78                      zmax=zmax_2D_temp)
79
80    def contextMenuQuickPlot(self):
81        """
82        Define context menu and associated actions for the quickplot MPL widget
83        """
84        # Actions
85        self.contextMenu = QtGui.QMenu(self)
86        self.actionSaveImage = self.contextMenu.addAction("Save Image")
87        self.actionPrintImage = self.contextMenu.addAction("Print Image")
88        self.actionCopyToClipboard = self.contextMenu.addAction("Copy to Clipboard")
89        self.contextMenu.addSeparator()
90        if self.dimension == 2:
91            self.actionToggleGrid = self.contextMenu.addAction("Toggle Grid On/Off")
92            self.contextMenu.addSeparator()
93        self.actionChangeScale = self.contextMenu.addAction("Toggle Linear/Log Scale")
94
95        # Define the callbacks
96        self.actionSaveImage.triggered.connect(self.onImageSave)
97        self.actionPrintImage.triggered.connect(self.onImagePrint)
98        self.actionCopyToClipboard.triggered.connect(self.onClipboardCopy)
99        if self.dimension == 2:
100            self.actionToggleGrid.triggered.connect(self.onGridToggle)
101        self.actionChangeScale.triggered.connect(self.onToggleScale)
102
103    def onToggleScale(self, event):
104        """
105        Toggle axis and replot image
106        """
107        self.plot()
108
109    def showPlot(self, data, qx_data, qy_data, xmin, xmax, ymin, ymax,
110                 zmin, zmax, color=0, symbol=0, markersize=0,
111                 label='data2D', cmap=DEFAULT_CMAP):
112        """
113        Render and show the current data
114        """
115        self.qx_data = qx_data
116        self.qy_data = qy_data
117        self.xmin = xmin
118        self.xmax = xmax
119        self.ymin = ymin
120        self.ymax = ymax
121        self.zmin = zmin
122        self.zmax = zmax
123        # If we don't have any data, skip.
124        if data is None:
125            return
126        if data.ndim == 1:
127            output = PlotUtilities.build_matrix(data, self.qx_data, self.qy_data)
128        else:
129            output = copy.deepcopy(data)
130
131        zmin_temp = self.zmin
132        # check scale
133        if self.scale == 'log_{10}':
134            try:
135                if  self.zmin <= 0  and len(output[output > 0]) > 0:
136                    zmin_temp = self.zmin_2D
137                    output[output > 0] = numpy.log10(output[output > 0])
138                elif self.zmin <= 0:
139                    zmin_temp = self.zmin
140                    output[output > 0] = numpy.zeros(len(output))
141                    output[output <= 0] = MIN_Z
142                else:
143                    zmin_temp = self.zmin
144                    output[output > 0] = numpy.log10(output[output > 0])
145            except:
146                #Too many problems in 2D plot with scale
147                output[output > 0] = numpy.log10(output[output > 0])
148                pass
149
150        self.cmap = cmap
151        if self.dimension != 3:
152            #Re-adjust colorbar
153            self.figure.subplots_adjust(left=0.2, right=.8, bottom=.2)
154
155            im = self.ax.imshow(output, interpolation='nearest',
156                                origin='lower',
157                                vmin=zmin_temp, vmax=self.zmax,
158                                cmap=self.cmap,
159                                extent=(self.xmin, self.xmax,
160                                        self.ymin, self.ymax))
161
162            cbax = self.figure.add_axes([0.84, 0.2, 0.02, 0.7])
163
164            # Current labels for axes
165            self.ax.set_ylabel(self.y_label)
166            self.ax.set_xlabel(self.x_label)
167
168            # Title only for regular charts
169            if not self.quickplot:
170                self.ax.set_title(label=self._title)
171
172            if cbax is None:
173                ax.set_frame_on(False)
174                cb = self.figure.colorbar(im, shrink=0.8, aspect=20)
175            else:
176                cb = self.figure.colorbar(im, cax=cbax)
177
178            cb.update_bruteforce(im)
179            cb.set_label('$' + self.scale + '$')
180
181        else:
182            # clear the previous 2D from memory
183            self.figure.clear()
184
185            self.figure.subplots_adjust(left=0.1, right=.8, bottom=.1)
186
187            X = self._data.x_bins[0:-1]
188            Y = self._data.y_bins[0:-1]
189            X, Y = numpy.meshgrid(X, Y)
190
191            ax = Axes3D(self.figure)
192            #cbax = self.figure.add_axes([0.84, 0.1, 0.02, 0.8])
193
194            # Disable rotation for large sets.
195            # TODO: Define "large" for a dataset
196            SET_TOO_LARGE = 500
197            if len(X) > SET_TOO_LARGE:
198                ax.disable_mouse_rotation()
199
200            self.figure.canvas.resizing = False
201            im = ax.plot_surface(X, Y, output, rstride=1, cstride=1, cmap=cmap,
202                                 linewidth=0, antialiased=False)
203            self.ax.set_axis_off()
204
205        if self.dimension != 3:
206            self.figure.canvas.draw_idle()
207        else:
208            self.figure.canvas.draw()
209
210class Plotter2D(QtGui.QDialog, Plotter2DWidget):
211    def __init__(self, parent=None, quickplot=False, dimension=2):
212
213        QtGui.QDialog.__init__(self)
214        Plotter2DWidget.__init__(self, manager=parent, quickplot=quickplot, dimension=dimension)
215        icon = QtGui.QIcon()
216        icon.addPixmap(QtGui.QPixmap(":/res/ball.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
217        self.setWindowIcon(icon)
Note: See TracBrowser for help on using the repository browser.