From 65056f02f41372977a98b322ea5011d81b10e7e0 Mon Sep 17 00:00:00 2001
From: Yngve Inntjore Levinsen <Yngve.Levinsen@esss.se>
Date: Wed, 27 Jul 2016 10:25:42 +0200
Subject: [PATCH] allow dst.subplot to only take one variable (1D histogram)
 fix for IS energy levels (use keV instead of MeV when max(E)<0.1)

---
 ess/TraceWin.py | 38 ++++++++++++++++++++++++++------------
 1 file changed, 26 insertions(+), 12 deletions(-)

diff --git a/ess/TraceWin.py b/ess/TraceWin.py
index 9e14096..c195492 100644
--- a/ess/TraceWin.py
+++ b/ess/TraceWin.py
@@ -149,7 +149,7 @@ class dst:
         print >>fout, out
         fout.close()
 
-    def subplot(self, index, x, y, nb=100, mask=None):
+    def subplot(self, index, x, y=None, nb=100, mask=None):
         '''
         Create a subplot histogram similar to TraceWin.
 
@@ -175,11 +175,13 @@ class dst:
                 'E': 'MeV', 'phi': 'deg'
               }
         # get X and Y data
-        dx=self[x]
-        dy=self[y]
+        dx=np.array(self[x])
         if mask!=None:
             dx=dx[mask]
-            dy=dy[mask]
+        if y!=None:
+            dy=np.array(self[y])
+            if mask!=None:
+                dy=dy[mask]
 
         if x in ['x','y','xp','yp']:
             dx*=1e3
@@ -191,16 +193,28 @@ class dst:
         if y in ['phi']:
             dy-=np.average(dy)
             dy*=180/np.pi
+        if x in ['E'] and max(dx)<0.1:
+            dx*=1e3
+            units['E']='keV'
+        if y in ['E'] and max(dy)<0.1:
+            dy*=1e3
+            units['E']='keV'
+    
 
         plt.subplot(index)
-        plt.hist2d(dx, dy, bins=nb, norm=LogNorm())
-        plt.title('{} [{}] - {} [{}]'.format(x,units[x],y,units[y]))
-        hist,bin_edges=np.histogram(dx,bins=nb)
-        b=bin_edges[:-1]+0.5*(bin_edges[1]-bin_edges[0])
-        plt.plot(b,hist*0.2*(max(dy)-min(dy))/max(hist)+min(dy),'k',lw=1.5,drawstyle='steps')
-        hist,bin_edges=np.histogram(dy,bins=nb)
-        b=bin_edges[:-1]+0.0*(bin_edges[1]-bin_edges[0])
-        plt.plot(hist*0.2*(max(dx)-min(dx))/max(hist)+min(dx),b,'k',lw=1.5,drawstyle='steps')
+        if y!=None:
+            plt.hist2d(dx, dy, bins=nb, norm=LogNorm())
+            plt.title('{} [{}] - {} [{}]'.format(x,units[x],y,units[y]))
+            hist,bin_edges=np.histogram(dx,bins=nb)
+            b=bin_edges[:-1]+0.5*(bin_edges[1]-bin_edges[0])
+            plt.plot(b,hist*0.2*(max(dy)-min(dy))/max(hist)+min(dy),'k',lw=1.5,drawstyle='steps')
+            hist,bin_edges=np.histogram(dy,bins=nb)
+            b=bin_edges[:-1]+0.0*(bin_edges[1]-bin_edges[0])
+            plt.plot(hist*0.2*(max(dx)-min(dx))/max(hist)+min(dx),b,'k',lw=1.5,drawstyle='steps')
+        else:
+            # plot a simple 1D histogram..
+            plt.hist(dx, bins=nb)
+            plt.title('{} [{}]'.format(x,units[x]))
 
 class plt:
     '''
-- 
GitLab