1 | from PyQt5 import QtCore |
---|
2 | from PyQt5 import QtGui |
---|
3 | from PyQt5 import QtWidgets |
---|
4 | |
---|
5 | import functools |
---|
6 | import copy |
---|
7 | import matplotlib as mpl |
---|
8 | import numpy as np |
---|
9 | from matplotlib.font_manager import FontProperties |
---|
10 | |
---|
11 | from sas.qtgui.Plotting.PlotterData import Data1D |
---|
12 | from sas.qtgui.Plotting.PlotterBase import PlotterBase |
---|
13 | from sas.qtgui.Plotting.AddText import AddText |
---|
14 | from sas.qtgui.Plotting.SetGraphRange import SetGraphRange |
---|
15 | from sas.qtgui.Plotting.LinearFit import LinearFit |
---|
16 | from sas.qtgui.Plotting.PlotProperties import PlotProperties |
---|
17 | from sas.qtgui.Plotting.ScaleProperties import ScaleProperties |
---|
18 | |
---|
19 | import sas.qtgui.Utilities.GuiUtils as GuiUtils |
---|
20 | import sas.qtgui.Plotting.PlotUtilities as PlotUtilities |
---|
21 | |
---|
22 | class PlotterWidget(PlotterBase): |
---|
23 | """ |
---|
24 | 1D Plot widget for use with a QDialog |
---|
25 | """ |
---|
26 | def __init__(self, parent=None, manager=None, quickplot=False): |
---|
27 | super(PlotterWidget, self).__init__(parent, manager=manager, quickplot=quickplot) |
---|
28 | |
---|
29 | self.parent = parent |
---|
30 | |
---|
31 | # Dictionary of {plot_id:Data1d} |
---|
32 | self.plot_dict = {} |
---|
33 | # Dictionaty of {plot_id:line} |
---|
34 | |
---|
35 | self.plot_lines = {} |
---|
36 | # Window for text add |
---|
37 | self.addText = AddText(self) |
---|
38 | |
---|
39 | # Log-ness of the axes |
---|
40 | self.xLogLabel = "log10(x)" |
---|
41 | self.yLogLabel = "log10(y)" |
---|
42 | |
---|
43 | # Data container for the linear fit |
---|
44 | self.fit_result = Data1D(x=[], y=[], dy=None) |
---|
45 | self.fit_result.symbol = 13 |
---|
46 | self.fit_result.name = "Fit" |
---|
47 | |
---|
48 | @property |
---|
49 | def data(self): |
---|
50 | return self._data |
---|
51 | |
---|
52 | @data.setter |
---|
53 | def data(self, value): |
---|
54 | """ data setter """ |
---|
55 | self._data = value |
---|
56 | if value._xunit: |
---|
57 | self.xLabel = "%s(%s)"%(value._xaxis, value._xunit) |
---|
58 | else: |
---|
59 | self.xLabel = "%s"%(value._xaxis) |
---|
60 | if value._yunit: |
---|
61 | self.yLabel = "%s(%s)"%(value._yaxis, value._yunit) |
---|
62 | else: |
---|
63 | self.yLabel = "%s"%(value._yaxis) |
---|
64 | |
---|
65 | if value.scale == 'linear' or value.isSesans: |
---|
66 | self.xscale = 'linear' |
---|
67 | self.yscale = 'linear' |
---|
68 | self.title(title=value.name) |
---|
69 | |
---|
70 | def plot(self, data=None, color=None, marker=None, hide_error=False, transform=True): |
---|
71 | """ |
---|
72 | Add a new plot of self._data to the chart. |
---|
73 | """ |
---|
74 | # Data1D |
---|
75 | if isinstance(data, Data1D): |
---|
76 | self.data = data |
---|
77 | |
---|
78 | if not self._data: |
---|
79 | return |
---|
80 | |
---|
81 | is_fit = (self.data.id=="fit") |
---|
82 | |
---|
83 | if not is_fit: |
---|
84 | # make sure we have some function to operate on |
---|
85 | if self.data.xtransform is None: |
---|
86 | self.data.xtransform = 'log10(x)' |
---|
87 | if self.data.ytransform is None: |
---|
88 | self.data.ytransform = 'log10(y)' |
---|
89 | #Added condition to Dmax explorer from P(r) perspective |
---|
90 | if self.data._xaxis == 'D_{max}': |
---|
91 | self.xscale = 'linear' |
---|
92 | # Transform data if required. |
---|
93 | if transform and (self.data.xtransform is not None or self.data.ytransform is not None): |
---|
94 | _, _, xscale, yscale = GuiUtils.xyTransform(self.data, self.data.xtransform, self.data.ytransform) |
---|
95 | if xscale != 'log': |
---|
96 | self.xscale = xscale |
---|
97 | if yscale != 'log': |
---|
98 | self.yscale = yscale |
---|
99 | |
---|
100 | # Redefine the Scale properties dialog |
---|
101 | self.properties = ScaleProperties(self, |
---|
102 | init_scale_x=self.data.xtransform, |
---|
103 | init_scale_y=self.data.ytransform) |
---|
104 | |
---|
105 | # Shortcuts |
---|
106 | ax = self.ax |
---|
107 | x = self._data.view.x |
---|
108 | y = self._data.view.y |
---|
109 | |
---|
110 | # Marker symbol. Passed marker is one of matplotlib.markers characters |
---|
111 | # Alternatively, picked up from Data1D as an int index of PlotUtilities.SHAPES dict |
---|
112 | if marker is None: |
---|
113 | marker = self.data.symbol |
---|
114 | # Try name first |
---|
115 | try: |
---|
116 | marker = dict(PlotUtilities.SHAPES)[marker] |
---|
117 | except KeyError: |
---|
118 | marker = list(PlotUtilities.SHAPES.values())[marker] |
---|
119 | |
---|
120 | assert marker is not None |
---|
121 | # Plot name |
---|
122 | if self.data.title: |
---|
123 | self.title(title=self.data.title) |
---|
124 | else: |
---|
125 | self.title(title=self.data.name) |
---|
126 | |
---|
127 | # Error marker toggle |
---|
128 | if hide_error is None: |
---|
129 | hide_error = self.data.hide_error |
---|
130 | |
---|
131 | # Plot color |
---|
132 | if color is None: |
---|
133 | color = self.data.custom_color |
---|
134 | |
---|
135 | color = PlotUtilities.getValidColor(color) |
---|
136 | self.data.custom_color = color |
---|
137 | |
---|
138 | markersize = self._data.markersize |
---|
139 | |
---|
140 | # Include scaling (log vs. linear) |
---|
141 | ax.set_xscale(self.xscale, nonposx='clip') |
---|
142 | ax.set_yscale(self.yscale, nonposy='clip') |
---|
143 | |
---|
144 | # define the ranges |
---|
145 | self.setRange = SetGraphRange(parent=self, |
---|
146 | x_range=self.ax.get_xlim(), y_range=self.ax.get_ylim()) |
---|
147 | |
---|
148 | # Draw non-standard markers |
---|
149 | l_width = markersize * 0.4 |
---|
150 | if marker == '-' or marker == '--': |
---|
151 | line = self.ax.plot(x, y, color=color, lw=l_width, marker='', |
---|
152 | linestyle=marker, label=self._title, zorder=10)[0] |
---|
153 | |
---|
154 | elif marker == 'vline': |
---|
155 | y_min = min(y)*9.0/10.0 if min(y) < 0 else 0.0 |
---|
156 | line = self.ax.vlines(x=x, ymin=y_min, ymax=y, color=color, |
---|
157 | linestyle='-', label=self._title, lw=l_width, zorder=1) |
---|
158 | |
---|
159 | elif marker == 'step': |
---|
160 | line = self.ax.step(x, y, color=color, marker='', linestyle='-', |
---|
161 | label=self._title, lw=l_width, zorder=1)[0] |
---|
162 | |
---|
163 | else: |
---|
164 | # plot data with/without errorbars |
---|
165 | if hide_error: |
---|
166 | line = ax.plot(x, y, marker=marker, color=color, markersize=markersize, |
---|
167 | linestyle='', label=self._title, picker=True) |
---|
168 | else: |
---|
169 | dy = self._data.view.dy |
---|
170 | # Convert tuple (lo,hi) to array [(x-lo),(hi-x)] |
---|
171 | if dy is not None and type(dy) == type(()): |
---|
172 | dy = np.vstack((y - dy[0], dy[1] - y)).transpose() |
---|
173 | |
---|
174 | line = ax.errorbar(x, y, |
---|
175 | yerr=dy, |
---|
176 | xerr=None, |
---|
177 | capsize=2, linestyle='', |
---|
178 | barsabove=False, |
---|
179 | color=color, |
---|
180 | marker=marker, |
---|
181 | markersize=markersize, |
---|
182 | lolims=False, uplims=False, |
---|
183 | xlolims=False, xuplims=False, |
---|
184 | label=self._title, |
---|
185 | zorder=1, |
---|
186 | picker=True) |
---|
187 | |
---|
188 | # Update the list of data sets (plots) in chart |
---|
189 | self.plot_dict[self._data.name] = self.data |
---|
190 | |
---|
191 | self.plot_lines[self._data.name] = line |
---|
192 | |
---|
193 | # Now add the legend with some customizations. |
---|
194 | |
---|
195 | if self.showLegend: |
---|
196 | self.legend = ax.legend(loc='upper right', shadow=True) |
---|
197 | if self.legend: |
---|
198 | self.legend.set_picker(True) |
---|
199 | |
---|
200 | # Current labels for axes |
---|
201 | if self.y_label and not is_fit: |
---|
202 | ax.set_ylabel(self.y_label) |
---|
203 | if self.x_label and not is_fit: |
---|
204 | ax.set_xlabel(self.x_label) |
---|
205 | |
---|
206 | # refresh canvas |
---|
207 | self.canvas.draw_idle() |
---|
208 | |
---|
209 | def createContextMenu(self): |
---|
210 | """ |
---|
211 | Define common context menu and associated actions for the MPL widget |
---|
212 | """ |
---|
213 | self.defaultContextMenu() |
---|
214 | |
---|
215 | # Separate plots |
---|
216 | self.addPlotsToContextMenu() |
---|
217 | |
---|
218 | # Additional menu items |
---|
219 | self.contextMenu.addSeparator() |
---|
220 | self.actionAddText = self.contextMenu.addAction("Add Text") |
---|
221 | self.actionRemoveText = self.contextMenu.addAction("Remove Text") |
---|
222 | self.contextMenu.addSeparator() |
---|
223 | self.actionChangeScale = self.contextMenu.addAction("Change Scale") |
---|
224 | self.contextMenu.addSeparator() |
---|
225 | self.actionSetGraphRange = self.contextMenu.addAction("Set Graph Range") |
---|
226 | self.actionResetGraphRange =\ |
---|
227 | self.contextMenu.addAction("Reset Graph Range") |
---|
228 | # Add the title change for dialogs |
---|
229 | self.contextMenu.addSeparator() |
---|
230 | self.actionWindowTitle = self.contextMenu.addAction("Window Title") |
---|
231 | |
---|
232 | # Define the callbacks |
---|
233 | self.actionAddText.triggered.connect(self.onAddText) |
---|
234 | self.actionRemoveText.triggered.connect(self.onRemoveText) |
---|
235 | self.actionChangeScale.triggered.connect(self.onScaleChange) |
---|
236 | self.actionSetGraphRange.triggered.connect(self.onSetGraphRange) |
---|
237 | self.actionResetGraphRange.triggered.connect(self.onResetGraphRange) |
---|
238 | self.actionWindowTitle.triggered.connect(self.onWindowsTitle) |
---|
239 | |
---|
240 | def addPlotsToContextMenu(self): |
---|
241 | """ |
---|
242 | Adds operations on all plotted sets of data to the context menu |
---|
243 | """ |
---|
244 | for id in list(self.plot_dict.keys()): |
---|
245 | plot = self.plot_dict[id] |
---|
246 | |
---|
247 | name = plot.name if plot.name else plot.title |
---|
248 | plot_menu = self.contextMenu.addMenu('&%s' % name) |
---|
249 | |
---|
250 | self.actionDataInfo = plot_menu.addAction("&DataInfo") |
---|
251 | self.actionDataInfo.triggered.connect( |
---|
252 | functools.partial(self.onDataInfo, plot)) |
---|
253 | |
---|
254 | self.actionSavePointsAsFile = plot_menu.addAction("&Save Points as a File") |
---|
255 | self.actionSavePointsAsFile.triggered.connect( |
---|
256 | functools.partial(self.onSavePoints, plot)) |
---|
257 | plot_menu.addSeparator() |
---|
258 | |
---|
259 | if plot.id != 'fit': |
---|
260 | self.actionLinearFit = plot_menu.addAction('&Linear Fit') |
---|
261 | self.actionLinearFit.triggered.connect( |
---|
262 | functools.partial(self.onLinearFit, id)) |
---|
263 | plot_menu.addSeparator() |
---|
264 | |
---|
265 | self.actionRemovePlot = plot_menu.addAction("Remove") |
---|
266 | self.actionRemovePlot.triggered.connect( |
---|
267 | functools.partial(self.onRemovePlot, id)) |
---|
268 | |
---|
269 | if not plot.is_data: |
---|
270 | self.actionFreeze = plot_menu.addAction('&Freeze') |
---|
271 | self.actionFreeze.triggered.connect( |
---|
272 | functools.partial(self.onFreeze, id)) |
---|
273 | plot_menu.addSeparator() |
---|
274 | |
---|
275 | if plot.is_data: |
---|
276 | self.actionHideError = plot_menu.addAction("Hide Error Bar") |
---|
277 | if plot.dy is not None and plot.dy != []: |
---|
278 | if plot.hide_error: |
---|
279 | self.actionHideError.setText('Show Error Bar') |
---|
280 | else: |
---|
281 | self.actionHideError.setEnabled(False) |
---|
282 | self.actionHideError.triggered.connect( |
---|
283 | functools.partial(self.onToggleHideError, id)) |
---|
284 | plot_menu.addSeparator() |
---|
285 | |
---|
286 | self.actionModifyPlot = plot_menu.addAction('&Modify Plot Property') |
---|
287 | self.actionModifyPlot.triggered.connect( |
---|
288 | functools.partial(self.onModifyPlot, id)) |
---|
289 | |
---|
290 | def createContextMenuQuick(self): |
---|
291 | """ |
---|
292 | Define context menu and associated actions for the quickplot MPL widget |
---|
293 | """ |
---|
294 | # Default actions |
---|
295 | self.defaultContextMenu() |
---|
296 | |
---|
297 | # Additional actions |
---|
298 | self.actionToggleGrid = self.contextMenu.addAction("Toggle Grid On/Off") |
---|
299 | self.contextMenu.addSeparator() |
---|
300 | self.actionChangeScale = self.contextMenu.addAction("Change Scale") |
---|
301 | |
---|
302 | # Define the callbacks |
---|
303 | self.actionToggleGrid.triggered.connect(self.onGridToggle) |
---|
304 | self.actionChangeScale.triggered.connect(self.onScaleChange) |
---|
305 | |
---|
306 | def onScaleChange(self): |
---|
307 | """ |
---|
308 | Show a dialog allowing axes rescaling |
---|
309 | """ |
---|
310 | if self.properties.exec_() == QtWidgets.QDialog.Accepted: |
---|
311 | self.xLogLabel, self.yLogLabel = self.properties.getValues() |
---|
312 | self.data.xtransform = self.xLogLabel |
---|
313 | self.data.ytransform = self.yLogLabel |
---|
314 | self.xyTransform(self.xLogLabel, self.yLogLabel) |
---|
315 | |
---|
316 | def onAddText(self): |
---|
317 | """ |
---|
318 | Show a dialog allowing adding custom text to the chart |
---|
319 | """ |
---|
320 | if self.addText.exec_() != QtWidgets.QDialog.Accepted: |
---|
321 | return |
---|
322 | |
---|
323 | # Retrieve the new text, its font and color |
---|
324 | extra_text = self.addText.text() |
---|
325 | extra_font = self.addText.font() |
---|
326 | extra_color = self.addText.color() |
---|
327 | |
---|
328 | # Place the text on the screen at the click location |
---|
329 | pos_x = self.x_click |
---|
330 | pos_y = self.y_click |
---|
331 | |
---|
332 | # Map QFont onto MPL font |
---|
333 | mpl_font = FontProperties() |
---|
334 | mpl_font.set_size(int(extra_font.pointSize())) |
---|
335 | mpl_font.set_family(str(extra_font.family())) |
---|
336 | mpl_font.set_weight(int(extra_font.weight())) |
---|
337 | # MPL style names |
---|
338 | styles = ['normal', 'italic', 'oblique'] |
---|
339 | # QFont::Style maps directly onto the above |
---|
340 | try: |
---|
341 | mpl_font.set_style(styles[extra_font.style()]) |
---|
342 | except: |
---|
343 | pass |
---|
344 | |
---|
345 | if len(extra_text) > 0: |
---|
346 | new_text = self.ax.text(pos_x, |
---|
347 | pos_y, |
---|
348 | extra_text, |
---|
349 | color=extra_color, |
---|
350 | fontproperties=mpl_font) |
---|
351 | |
---|
352 | # Update the list of annotations |
---|
353 | self.textList.append(new_text) |
---|
354 | self.canvas.draw_idle() |
---|
355 | |
---|
356 | def onRemoveText(self): |
---|
357 | """ |
---|
358 | Remove the most recently added text |
---|
359 | """ |
---|
360 | num_text = len(self.textList) |
---|
361 | if num_text < 1: |
---|
362 | return |
---|
363 | txt = self.textList[num_text - 1] |
---|
364 | text_remove = txt.get_text() |
---|
365 | try: |
---|
366 | txt.remove() |
---|
367 | except ValueError: |
---|
368 | # Text got already deleted somehow |
---|
369 | pass |
---|
370 | self.textList.remove(txt) |
---|
371 | |
---|
372 | self.canvas.draw_idle() |
---|
373 | |
---|
374 | def onSetGraphRange(self): |
---|
375 | """ |
---|
376 | Show a dialog allowing setting the chart ranges |
---|
377 | """ |
---|
378 | # min and max of data |
---|
379 | if self.setRange.exec_() == QtWidgets.QDialog.Accepted: |
---|
380 | x_range = self.setRange.xrange() |
---|
381 | y_range = self.setRange.yrange() |
---|
382 | if x_range is not None and y_range is not None: |
---|
383 | self.ax.set_xlim(x_range) |
---|
384 | self.ax.set_ylim(y_range) |
---|
385 | self.canvas.draw_idle() |
---|
386 | |
---|
387 | def onResetGraphRange(self): |
---|
388 | """ |
---|
389 | Resets the chart X and Y ranges to their original values |
---|
390 | """ |
---|
391 | x_range = (self.data.x.min(), self.data.x.max()) |
---|
392 | y_range = (self.data.y.min(), self.data.y.max()) |
---|
393 | if x_range is not None and y_range is not None: |
---|
394 | self.ax.set_xlim(x_range) |
---|
395 | self.ax.set_ylim(y_range) |
---|
396 | self.canvas.draw_idle() |
---|
397 | |
---|
398 | def onLinearFit(self, id): |
---|
399 | """ |
---|
400 | Creates and displays a simple linear fit for the selected plot |
---|
401 | """ |
---|
402 | selected_plot = self.plot_dict[id] |
---|
403 | |
---|
404 | maxrange = (min(selected_plot.x), max(selected_plot.x)) |
---|
405 | fitrange = self.ax.get_xlim() |
---|
406 | |
---|
407 | fit_dialog = LinearFit(parent=self, |
---|
408 | data=selected_plot, |
---|
409 | max_range=maxrange, |
---|
410 | fit_range=fitrange, |
---|
411 | xlabel=self.xLogLabel, |
---|
412 | ylabel=self.yLogLabel) |
---|
413 | fit_dialog.updatePlot.connect(self.onFitDisplay) |
---|
414 | if fit_dialog.exec_() == QtWidgets.QDialog.Accepted: |
---|
415 | return |
---|
416 | |
---|
417 | def replacePlot(self, id, new_plot): |
---|
418 | """ |
---|
419 | Remove plot 'id' and add 'new_plot' to the chart. |
---|
420 | This effectlvely refreshes the chart with changes to one of its plots |
---|
421 | """ |
---|
422 | self.removePlot(id) |
---|
423 | self.plot(data=new_plot) |
---|
424 | |
---|
425 | def onRemovePlot(self, id): |
---|
426 | """ |
---|
427 | Responds to the plot delete action |
---|
428 | """ |
---|
429 | self.removePlot(id) |
---|
430 | |
---|
431 | if len(self.plot_dict) == 0: |
---|
432 | # last plot: graph is empty must be the panel must be destroyed |
---|
433 | self.parent.close() |
---|
434 | |
---|
435 | def removePlot(self, id): |
---|
436 | """ |
---|
437 | Deletes the selected plot from the chart |
---|
438 | """ |
---|
439 | if id not in list(self.plot_dict.keys()): |
---|
440 | return |
---|
441 | |
---|
442 | selected_plot = self.plot_dict[id] |
---|
443 | |
---|
444 | plot_dict = copy.deepcopy(self.plot_dict) |
---|
445 | |
---|
446 | # Labels might have been changed |
---|
447 | xl = self.ax.xaxis.label.get_text() |
---|
448 | yl = self.ax.yaxis.label.get_text() |
---|
449 | |
---|
450 | self.plot_dict = {} |
---|
451 | |
---|
452 | mpl.pyplot.cla() |
---|
453 | self.ax.cla() |
---|
454 | |
---|
455 | for ids in plot_dict: |
---|
456 | if ids != id: |
---|
457 | self.plot(data=plot_dict[ids], hide_error=plot_dict[ids].hide_error) |
---|
458 | |
---|
459 | # Reset the labels |
---|
460 | self.ax.set_xlabel(xl) |
---|
461 | self.ax.set_ylabel(yl) |
---|
462 | self.canvas.draw_idle() |
---|
463 | |
---|
464 | def onFreeze(self, id): |
---|
465 | """ |
---|
466 | Freezes the selected plot to a separate chart |
---|
467 | """ |
---|
468 | plot = self.plot_dict[id] |
---|
469 | plot = copy.deepcopy(plot) |
---|
470 | self.manager.add_data(data_list={id:plot}) |
---|
471 | self.manager.freezeDataToItem(plot) |
---|
472 | |
---|
473 | def onModifyPlot(self, id): |
---|
474 | """ |
---|
475 | Allows for MPL modifications to the selected plot |
---|
476 | """ |
---|
477 | selected_plot = self.plot_dict[id] |
---|
478 | selected_line = self.plot_lines[id] |
---|
479 | # Old style color - single integer for enum color |
---|
480 | # New style color - #hhhhhh |
---|
481 | color = selected_plot.custom_color |
---|
482 | # marker symbol and size |
---|
483 | marker = selected_plot.symbol |
---|
484 | marker_size = selected_plot.markersize |
---|
485 | # plot name |
---|
486 | legend = selected_plot.title |
---|
487 | |
---|
488 | plotPropertiesWidget = PlotProperties(self, |
---|
489 | color=color, |
---|
490 | marker=marker, |
---|
491 | marker_size=marker_size, |
---|
492 | legend=legend) |
---|
493 | if plotPropertiesWidget.exec_() == QtWidgets.QDialog.Accepted: |
---|
494 | # Update Data1d |
---|
495 | selected_plot.markersize = plotPropertiesWidget.markersize() |
---|
496 | selected_plot.custom_color = plotPropertiesWidget.color() |
---|
497 | selected_plot.symbol = plotPropertiesWidget.marker() |
---|
498 | selected_plot.title = plotPropertiesWidget.legend() |
---|
499 | |
---|
500 | # Redraw the plot |
---|
501 | self.replacePlot(id, selected_plot) |
---|
502 | |
---|
503 | def onToggleHideError(self, id): |
---|
504 | """ |
---|
505 | Toggles hide error/show error menu item |
---|
506 | """ |
---|
507 | selected_plot = self.plot_dict[id] |
---|
508 | current = selected_plot.hide_error |
---|
509 | |
---|
510 | # Flip the flag |
---|
511 | selected_plot.hide_error = not current |
---|
512 | |
---|
513 | plot_dict = copy.deepcopy(self.plot_dict) |
---|
514 | self.plot_dict = {} |
---|
515 | |
---|
516 | # Clean the canvas |
---|
517 | mpl.pyplot.cla() |
---|
518 | self.ax.cla() |
---|
519 | |
---|
520 | # Recreate the plots but reverse the error flag for the current |
---|
521 | for ids in plot_dict: |
---|
522 | if ids == id: |
---|
523 | self.plot(data=plot_dict[ids], hide_error=(not current)) |
---|
524 | else: |
---|
525 | self.plot(data=plot_dict[ids], hide_error=plot_dict[ids].hide_error) |
---|
526 | |
---|
527 | def xyTransform(self, xLabel="", yLabel=""): |
---|
528 | """ |
---|
529 | Transforms x and y in View and set the scale |
---|
530 | """ |
---|
531 | # Transform all the plots on the chart |
---|
532 | for id in list(self.plot_dict.keys()): |
---|
533 | current_plot = self.plot_dict[id] |
---|
534 | if current_plot.id == "fit": |
---|
535 | self.removePlot(id) |
---|
536 | continue |
---|
537 | |
---|
538 | new_xlabel, new_ylabel, xscale, yscale =\ |
---|
539 | GuiUtils.xyTransform(current_plot, xLabel, yLabel) |
---|
540 | self.xscale = xscale |
---|
541 | self.yscale = yscale |
---|
542 | |
---|
543 | # Plot the updated chart |
---|
544 | self.removePlot(id) |
---|
545 | |
---|
546 | # This assignment will wrap the label in Latex "$" |
---|
547 | self.xLabel = new_xlabel |
---|
548 | self.yLabel = new_ylabel |
---|
549 | |
---|
550 | self.plot(data=current_plot, transform=False) |
---|
551 | |
---|
552 | pass # debug hook |
---|
553 | |
---|
554 | def onFitDisplay(self, fit_data): |
---|
555 | """ |
---|
556 | Add a linear fitting line to the chart |
---|
557 | """ |
---|
558 | # Create new data structure with fitting result |
---|
559 | tempx = fit_data[0] |
---|
560 | tempy = fit_data[1] |
---|
561 | self.fit_result.x = [] |
---|
562 | self.fit_result.y = [] |
---|
563 | self.fit_result.x = tempx |
---|
564 | self.fit_result.y = tempy |
---|
565 | self.fit_result.dx = None |
---|
566 | self.fit_result.dy = None |
---|
567 | |
---|
568 | #Remove another Fit, if exists |
---|
569 | self.removePlot("Fit") |
---|
570 | |
---|
571 | self.fit_result.reset_view() |
---|
572 | #self.offset_graph() |
---|
573 | |
---|
574 | # Set plot properties |
---|
575 | self.fit_result.id = 'fit' |
---|
576 | self.fit_result.title = 'Fit' |
---|
577 | self.fit_result.name = 'Fit' |
---|
578 | |
---|
579 | # Plot the line |
---|
580 | self.plot(data=self.fit_result, marker='-', hide_error=True) |
---|
581 | |
---|
582 | def onMplMouseDown(self, event): |
---|
583 | """ |
---|
584 | Left button down and ready to drag |
---|
585 | """ |
---|
586 | # Check that the LEFT button was pressed |
---|
587 | if event.button != 1: |
---|
588 | return |
---|
589 | |
---|
590 | self.leftdown = True |
---|
591 | for text in self.textList: |
---|
592 | if text.contains(event)[0]: # If user has clicked on text |
---|
593 | self.selectedText = text |
---|
594 | return |
---|
595 | if event.inaxes is None: |
---|
596 | return |
---|
597 | try: |
---|
598 | self.x_click = float(event.xdata) # / size_x |
---|
599 | self.y_click = float(event.ydata) # / size_y |
---|
600 | except: |
---|
601 | self.position = None |
---|
602 | |
---|
603 | x_str = GuiUtils.formatNumber(self.x_click) |
---|
604 | y_str = GuiUtils.formatNumber(self.y_click) |
---|
605 | coord_str = "x: {}, y: {}".format(x_str, y_str) |
---|
606 | self.manager.communicator.statusBarUpdateSignal.emit(coord_str) |
---|
607 | |
---|
608 | def onMplMouseUp(self, event): |
---|
609 | """ |
---|
610 | Set the data coordinates of the click |
---|
611 | """ |
---|
612 | self.x_click = event.xdata |
---|
613 | self.y_click = event.ydata |
---|
614 | |
---|
615 | # Check that the LEFT button was released |
---|
616 | if event.button == 1: |
---|
617 | self.leftdown = False |
---|
618 | self.selectedText = None |
---|
619 | |
---|
620 | #release the legend |
---|
621 | if self.gotLegend == 1: |
---|
622 | self.gotLegend = 0 |
---|
623 | |
---|
624 | def onMplMouseMotion(self, event): |
---|
625 | """ |
---|
626 | Check if the left button is press and the mouse in moving. |
---|
627 | Compute delta for x and y coordinates and then perform the drag |
---|
628 | """ |
---|
629 | if self.gotLegend == 1 and self.leftdown: |
---|
630 | self.onLegendMotion(event) |
---|
631 | return |
---|
632 | |
---|
633 | #if self.leftdown and self.selectedText is not None: |
---|
634 | if not self.leftdown or self.selectedText is None: |
---|
635 | return |
---|
636 | # User has clicked on text and is dragging |
---|
637 | if event.inaxes is None: |
---|
638 | # User has dragged outside of axes |
---|
639 | self.selectedText = None |
---|
640 | else: |
---|
641 | # Only move text if mouse is within axes |
---|
642 | self.selectedText.set_position((event.xdata, event.ydata)) |
---|
643 | self.canvas.draw_idle() |
---|
644 | return |
---|
645 | |
---|
646 | def onMplPick(self, event): |
---|
647 | """ |
---|
648 | On pick legend |
---|
649 | """ |
---|
650 | legend = self.legend |
---|
651 | if event.artist != legend: |
---|
652 | return |
---|
653 | # Get the box of the legend. |
---|
654 | bbox = self.legend.get_window_extent() |
---|
655 | # Get mouse coordinates at time of pick. |
---|
656 | self.mouse_x = event.mouseevent.x |
---|
657 | self.mouse_y = event.mouseevent.y |
---|
658 | # Get legend coordinates at time of pick. |
---|
659 | self.legend_x = bbox.xmin |
---|
660 | self.legend_y = bbox.ymin |
---|
661 | # Indicate we picked up the legend. |
---|
662 | self.gotLegend = 1 |
---|
663 | |
---|
664 | #self.legend.legendPatch.set_alpha(0.5) |
---|
665 | |
---|
666 | def onLegendMotion(self, event): |
---|
667 | """ |
---|
668 | On legend in motion |
---|
669 | """ |
---|
670 | ax = event.inaxes |
---|
671 | if ax is None: |
---|
672 | return |
---|
673 | # Event occurred inside a plotting area |
---|
674 | lo_x, hi_x = ax.get_xlim() |
---|
675 | lo_y, hi_y = ax.get_ylim() |
---|
676 | # How much the mouse moved. |
---|
677 | x = mouse_diff_x = self.mouse_x - event.x |
---|
678 | y = mouse_diff_y = self.mouse_y - event.y |
---|
679 | # Put back inside |
---|
680 | if x < lo_x: |
---|
681 | x = lo_x |
---|
682 | if x > hi_x: |
---|
683 | x = hi_x |
---|
684 | if y < lo_y: |
---|
685 | y = lo_y |
---|
686 | if y > hi_y: |
---|
687 | y = hi_y |
---|
688 | # Move the legend from its previous location by that same amount |
---|
689 | loc_in_canvas = self.legend_x - mouse_diff_x, \ |
---|
690 | self.legend_y - mouse_diff_y |
---|
691 | # Transform into legend coordinate system |
---|
692 | trans_axes = self.legend.parent.transAxes.inverted() |
---|
693 | loc_in_norm_axes = trans_axes.transform_point(loc_in_canvas) |
---|
694 | self.legend_pos_loc = tuple(loc_in_norm_axes) |
---|
695 | self.legend._loc = self.legend_pos_loc |
---|
696 | self.canvas.draw_idle() |
---|
697 | |
---|
698 | def onMplWheel(self, event): |
---|
699 | """ |
---|
700 | Process mouse wheel as zoom events |
---|
701 | """ |
---|
702 | ax = event.inaxes |
---|
703 | step = event.step |
---|
704 | |
---|
705 | if ax is not None: |
---|
706 | # Event occurred inside a plotting area |
---|
707 | lo, hi = ax.get_xlim() |
---|
708 | lo, hi = PlotUtilities.rescale(lo, hi, step, |
---|
709 | pt=event.xdata, scale=ax.get_xscale()) |
---|
710 | if not self.xscale == 'log' or lo > 0: |
---|
711 | self._scale_xlo = lo |
---|
712 | self._scale_xhi = hi |
---|
713 | ax.set_xlim((lo, hi)) |
---|
714 | |
---|
715 | lo, hi = ax.get_ylim() |
---|
716 | lo, hi = PlotUtilities.rescale(lo, hi, step, pt=event.ydata, |
---|
717 | scale=ax.get_yscale()) |
---|
718 | if not self.yscale == 'log' or lo > 0: |
---|
719 | self._scale_ylo = lo |
---|
720 | self._scale_yhi = hi |
---|
721 | ax.set_ylim((lo, hi)) |
---|
722 | else: |
---|
723 | # Check if zoom happens in the axes |
---|
724 | xdata, ydata = None, None |
---|
725 | x, y = event.x, event.y |
---|
726 | |
---|
727 | for ax in self.axes: |
---|
728 | insidex, _ = ax.xaxis.contains(event) |
---|
729 | if insidex: |
---|
730 | xdata, _ = ax.transAxes.inverted().transform_point((x, y)) |
---|
731 | insidey, _ = ax.yaxis.contains(event) |
---|
732 | if insidey: |
---|
733 | _, ydata = ax.transAxes.inverted().transform_point((x, y)) |
---|
734 | if xdata is not None: |
---|
735 | lo, hi = ax.get_xlim() |
---|
736 | lo, hi = PlotUtilities.rescale(lo, hi, step, |
---|
737 | bal=xdata, scale=ax.get_xscale()) |
---|
738 | if not self.xscale == 'log' or lo > 0: |
---|
739 | self._scale_xlo = lo |
---|
740 | self._scale_xhi = hi |
---|
741 | ax.set_xlim((lo, hi)) |
---|
742 | if ydata is not None: |
---|
743 | lo, hi = ax.get_ylim() |
---|
744 | lo, hi = PlotUtilities.rescale(lo, hi, step, bal=ydata, |
---|
745 | scale=ax.get_yscale()) |
---|
746 | if not self.yscale == 'log' or lo > 0: |
---|
747 | self._scale_ylo = lo |
---|
748 | self._scale_yhi = hi |
---|
749 | ax.set_ylim((lo, hi)) |
---|
750 | self.canvas.draw_idle() |
---|
751 | |
---|
752 | |
---|
753 | class Plotter(QtWidgets.QDialog, PlotterWidget): |
---|
754 | def __init__(self, parent=None, quickplot=False): |
---|
755 | |
---|
756 | QtWidgets.QDialog.__init__(self) |
---|
757 | PlotterWidget.__init__(self, parent=self, manager=parent, quickplot=quickplot) |
---|
758 | icon = QtGui.QIcon() |
---|
759 | icon.addPixmap(QtGui.QPixmap(":/res/ball.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off) |
---|
760 | self.setWindowIcon(icon) |
---|
761 | |
---|
762 | |
---|