Changeset 6d05e1d in sasview for src/sas/qtgui/Plotter.py


Ignore:
Timestamp:
Dec 6, 2016 3:39:24 AM (7 years ago)
Author:
Piotr Rozyczko <rozyczko@…>
Branches:
ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc
Children:
55d89f8
Parents:
b94889a
git-author:
Piotr Rozyczko <rozyczko@…> (12/06/16 03:38:55)
git-committer:
Piotr Rozyczko <rozyczko@…> (12/06/16 03:39:24)
Message:

More functionality for quick plots + unit tests

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/qtgui/Plotter.py

    ref01be4 r6d05e1d  
    1 import logging 
    2  
    31from PyQt4 import QtGui 
    42 
    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 # 
    9 from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas 
    10 from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar 
    113import matplotlib.pyplot as plt 
    12 import matplotlib as mpl 
    13  
    14 import sas.qtgui.PlotHelper as PlotHelper 
     4 
     5from sas.sasgui.plottools import transform 
     6from sas.sasgui.plottools.convert_units import convert_unit 
    157from sas.qtgui.PlotterBase import PlotterBase 
    168 
     
    2719        """ data setter """ 
    2820        self._data = value 
    29         self.label=value.name 
    30         self.xLabel(xlabel="%s(%s)"%(value._xaxis, value._xunit)) 
    31         self.yLabel(ylabel="%s(%s)"%(value._yaxis, value._yunit)) 
     21        self.xLabel = "%s(%s)"%(value._xaxis, value._xunit) 
     22        self.yLabel = "%s(%s)"%(value._yaxis, value._yunit) 
    3223        self.title(title=value.title) 
    3324 
     
    3627        Plot self._data 
    3728        """ 
    38         # create an axis 
     29        # Shortcut for an axis 
    3930        ax = self.ax 
    4031 
    4132        if marker == None: 
    42             marker = '*' 
     33            marker = 'o' 
    4334 
    4435        if linestyle == None: 
    4536            linestyle = '-' 
    4637 
    47         # plot data with legend 
    48         ax.plot(self._data.x, self._data.y, marker=marker, linestyle=linestyle, label=self.title) 
     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) 
    4944 
    5045        # Now add the legend with some customizations. 
    5146        legend = ax.legend(loc='upper right', shadow=True) 
    5247 
     48        # Current labels for axes 
    5349        ax.set_ylabel(self.y_label) 
    5450        ax.set_xlabel(self.x_label) 
     51 
    5552        # Title only for regular charts 
    5653        if not self.quickplot: 
    57             ax.set_title(label=self.title) 
    58  
    59         ax.set_yscale('log') 
    60         ax.set_xscale('log') 
     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) 
    6159 
    6260        # refresh canvas 
    6361        self.canvas.draw() 
    6462 
     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='') 
Note: See TracChangeset for help on using the changeset viewer.