1 | from PyQt4 import QtGui |
---|
2 | |
---|
3 | import matplotlib.pyplot as plt |
---|
4 | |
---|
5 | from sas.sasgui.plottools import transform |
---|
6 | from sas.sasgui.plottools.convert_units import convert_unit |
---|
7 | from sas.qtgui.PlotterBase import PlotterBase |
---|
8 | |
---|
9 | class PlotterWidget(PlotterBase): |
---|
10 | def __init__(self, parent=None, manager=None, quickplot=False): |
---|
11 | super(PlotterWidget, self).__init__(parent, manager=manager, quickplot=quickplot) |
---|
12 | |
---|
13 | @property |
---|
14 | def data(self): |
---|
15 | return self._data |
---|
16 | |
---|
17 | @data.setter |
---|
18 | def data(self, value): |
---|
19 | """ data setter """ |
---|
20 | self._data = value |
---|
21 | self.xLabel = "%s(%s)"%(value._xaxis, value._xunit) |
---|
22 | self.yLabel = "%s(%s)"%(value._yaxis, value._yunit) |
---|
23 | self.title(title=value.title) |
---|
24 | |
---|
25 | def plot(self, marker=None, linestyle=None): |
---|
26 | """ |
---|
27 | Plot self._data |
---|
28 | """ |
---|
29 | # Shortcut for an axis |
---|
30 | ax = self.ax |
---|
31 | |
---|
32 | if marker == None: |
---|
33 | marker = 'o' |
---|
34 | |
---|
35 | if linestyle == None: |
---|
36 | linestyle = '-' |
---|
37 | |
---|
38 | # plot data with title |
---|
39 | ax.plot(self._data.view.x, |
---|
40 | self._data.view.y, |
---|
41 | marker=marker, |
---|
42 | linestyle=linestyle, |
---|
43 | label=self._title) |
---|
44 | |
---|
45 | # Now add the legend with some customizations. |
---|
46 | legend = ax.legend(loc='upper right', shadow=True) |
---|
47 | |
---|
48 | # Current labels for axes |
---|
49 | ax.set_ylabel(self.y_label) |
---|
50 | ax.set_xlabel(self.x_label) |
---|
51 | |
---|
52 | # Title only for regular charts |
---|
53 | if not self.quickplot: |
---|
54 | ax.set_title(label=self._title) |
---|
55 | |
---|
56 | # Include scaling (log vs. linear) |
---|
57 | ax.set_yscale(self.xscale) |
---|
58 | ax.set_xscale(self.xscale) |
---|
59 | |
---|
60 | # refresh canvas |
---|
61 | self.canvas.draw() |
---|
62 | |
---|
63 | def contextMenuQuickPlot(self): |
---|
64 | """ |
---|
65 | Define context menu and associated actions for the quickplot MPL widget |
---|
66 | """ |
---|
67 | # Actions |
---|
68 | self.contextMenu = QtGui.QMenu(self) |
---|
69 | self.actionSaveImage = self.contextMenu.addAction("Save Image") |
---|
70 | self.actionPrintImage = self.contextMenu.addAction("Print Image") |
---|
71 | self.actionCopyToClipboard = self.contextMenu.addAction("Copy to Clipboard") |
---|
72 | self.contextMenu.addSeparator() |
---|
73 | self.actionToggleGrid = self.contextMenu.addAction("Toggle Grid On/Off") |
---|
74 | self.contextMenu.addSeparator() |
---|
75 | self.actionChangeScale = self.contextMenu.addAction("Change Scale") |
---|
76 | |
---|
77 | # Define the callbacks |
---|
78 | self.actionSaveImage.triggered.connect(self.onImageSave) |
---|
79 | self.actionPrintImage.triggered.connect(self.onImagePrint) |
---|
80 | self.actionCopyToClipboard.triggered.connect(self.onClipboardCopy) |
---|
81 | self.actionToggleGrid.triggered.connect(self.onGridToggle) |
---|
82 | self.actionChangeScale.triggered.connect(self.onScaleChange) |
---|
83 | |
---|
84 | def onScaleChange(self): |
---|
85 | """ |
---|
86 | Show a dialog allowing axes rescaling |
---|
87 | """ |
---|
88 | if self.properties.exec_() == QtGui.QDialog.Accepted: |
---|
89 | xLabel, yLabel = self.properties.getValues() |
---|
90 | self.xyTransform(xLabel, yLabel) |
---|
91 | |
---|
92 | def xyTransform(self, xLabel="", yLabel=""): |
---|
93 | """ |
---|
94 | Transforms x and y in View and set the scale |
---|
95 | """ |
---|
96 | # Clear the plot first |
---|
97 | plt.cla() |
---|
98 | |
---|
99 | # Changing the scale might be incompatible with |
---|
100 | # currently displayed data (for instance, going |
---|
101 | # from ln to log when all plotted values have |
---|
102 | # negative natural logs). |
---|
103 | # Go linear and only change the scale at the end. |
---|
104 | self._xscale = "linear" |
---|
105 | self._yscale = "linear" |
---|
106 | _xscale = 'linear' |
---|
107 | _yscale = 'linear' |
---|
108 | # Local data is either 1D or 2D |
---|
109 | if self.data.id == 'fit': |
---|
110 | return |
---|
111 | |
---|
112 | # control axis labels from the panel itself |
---|
113 | yname, yunits = self.data.get_yaxis() |
---|
114 | xname, xunits = self.data.get_xaxis() |
---|
115 | |
---|
116 | # Goes through all possible scales |
---|
117 | # self.x_label is already wrapped with Latex "$", so using the argument |
---|
118 | |
---|
119 | # X |
---|
120 | if xLabel == "x": |
---|
121 | self.data.transformX(transform.toX, transform.errToX) |
---|
122 | self.xLabel = "%s(%s)" % (xname, xunits) |
---|
123 | if xLabel == "x^(2)": |
---|
124 | self.data.transformX(transform.toX2, transform.errToX2) |
---|
125 | xunits = convert_unit(2, xunits) |
---|
126 | self.xLabel = "%s^{2}(%s)" % (xname, xunits) |
---|
127 | if xLabel == "x^(4)": |
---|
128 | self.data.transformX(transform.toX4, transform.errToX4) |
---|
129 | xunits = convert_unit(4, xunits) |
---|
130 | self.xLabel = "%s^{4}(%s)" % (xname, xunits) |
---|
131 | if xLabel == "ln(x)": |
---|
132 | self.data.transformX(transform.toLogX, transform.errToLogX) |
---|
133 | self.xLabel = "\ln{(%s)}(%s)" % (xname, xunits) |
---|
134 | if xLabel == "log10(x)": |
---|
135 | self.data.transformX(transform.toX_pos, transform.errToX_pos) |
---|
136 | _xscale = 'log' |
---|
137 | self.xLabel = "%s(%s)" % (xname, xunits) |
---|
138 | if xLabel == "log10(x^(4))": |
---|
139 | self.data.transformX(transform.toX4, transform.errToX4) |
---|
140 | xunits = convert_unit(4, xunits) |
---|
141 | self.xLabel = "%s^{4}(%s)" % (xname, xunits) |
---|
142 | _xscale = 'log' |
---|
143 | |
---|
144 | # Y |
---|
145 | if yLabel == "ln(y)": |
---|
146 | self.data.transformY(transform.toLogX, transform.errToLogX) |
---|
147 | self.yLabel = "\ln{(%s)}(%s)" % (yname, yunits) |
---|
148 | if yLabel == "y": |
---|
149 | self.data.transformY(transform.toX, transform.errToX) |
---|
150 | self.yLabel = "%s(%s)" % (yname, yunits) |
---|
151 | if yLabel == "log10(y)": |
---|
152 | self.data.transformY(transform.toX_pos, transform.errToX_pos) |
---|
153 | _yscale = 'log' |
---|
154 | self.yLabel = "%s(%s)" % (yname, yunits) |
---|
155 | if yLabel == "y^(2)": |
---|
156 | self.data.transformY(transform.toX2, transform.errToX2) |
---|
157 | yunits = convert_unit(2, yunits) |
---|
158 | self.yLabel = "%s^{2}(%s)" % (yname, yunits) |
---|
159 | if yLabel == "1/y": |
---|
160 | self.data.transformY(transform.toOneOverX, transform.errOneOverX) |
---|
161 | yunits = convert_unit(-1, yunits) |
---|
162 | self.yLabel = "1/%s(%s)" % (yname, yunits) |
---|
163 | if yLabel == "y*x^(2)": |
---|
164 | self.data.transformY(transform.toYX2, transform.errToYX2) |
---|
165 | xunits = convert_unit(2, xunits) |
---|
166 | self.yLabel = "%s \ \ %s^{2}(%s%s)" % (yname, xname, yunits, xunits) |
---|
167 | if yLabel == "y*x^(4)": |
---|
168 | self.data.transformY(transform.toYX4, transform.errToYX4) |
---|
169 | xunits = convert_unit(4, xunits) |
---|
170 | self.yLabel = "%s \ \ %s^{4}(%s%s)" % (yname, xname, yunits, xunits) |
---|
171 | if yLabel == "1/sqrt(y)": |
---|
172 | self.data.transformY(transform.toOneOverSqrtX, |
---|
173 | transform.errOneOverSqrtX) |
---|
174 | yunits = convert_unit(-0.5, yunits) |
---|
175 | self.yLabel = "1/\sqrt{%s}(%s)" % (yname, yunits) |
---|
176 | if yLabel == "ln(y*x)": |
---|
177 | self.data.transformY(transform.toLogXY, transform.errToLogXY) |
---|
178 | self.yLabel = "\ln{(%s \ \ %s)}(%s%s)" % (yname, xname, yunits, xunits) |
---|
179 | if yLabel == "ln(y*x^(2))": |
---|
180 | self.data.transformY(transform.toLogYX2, transform.errToLogYX2) |
---|
181 | xunits = convert_unit(2, xunits) |
---|
182 | self.yLabel = "\ln (%s \ \ %s^{2})(%s%s)" % (yname, xname, yunits, xunits) |
---|
183 | if yLabel == "ln(y*x^(4))": |
---|
184 | self.data.transformY(transform.toLogYX4, transform.errToLogYX4) |
---|
185 | xunits = convert_unit(4, xunits) |
---|
186 | self.yLabel = "\ln (%s \ \ %s^{4})(%s%s)" % (yname, xname, yunits, xunits) |
---|
187 | if yLabel == "log10(y*x^(4))": |
---|
188 | self.data.transformY(transform.toYX4, transform.errToYX4) |
---|
189 | xunits = convert_unit(4, xunits) |
---|
190 | _yscale = 'log' |
---|
191 | self.yLabel = "%s \ \ %s^{4}(%s%s)" % (yname, xname, yunits, xunits) |
---|
192 | |
---|
193 | # Perform the transformation of data in data1d->View |
---|
194 | self.data.transformView() |
---|
195 | |
---|
196 | self.xscale = _xscale |
---|
197 | self.yscale = _yscale |
---|
198 | |
---|
199 | # Plot the updated chart |
---|
200 | self.plot(marker='o', linestyle='') |
---|
201 | |
---|
202 | |
---|
203 | class Plotter(QtGui.QDialog, PlotterWidget): |
---|
204 | def __init__(self, parent=None, quickplot=False): |
---|
205 | |
---|
206 | QtGui.QDialog.__init__(self) |
---|
207 | PlotterWidget.__init__(self, manager=parent, quickplot=quickplot) |
---|
208 | |
---|