climate-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From good...@apache.org
Subject svn commit: r1508985 - /incubator/climate/branches/RefactorInput/ocw/plotter.py
Date Wed, 31 Jul 2013 18:44:18 GMT
Author: goodman
Date: Wed Jul 31 18:44:17 2013
New Revision: 1508985

URL: http://svn.apache.org/r1508985
Log:
CLIMATE-234 - Made changes to plotting functions consistent with API documentation
-Changed the name of 'datasets' parameter to 'results'
-Added aspect keyword to control axes aspect ratio

Modified:
    incubator/climate/branches/RefactorInput/ocw/plotter.py

Modified: incubator/climate/branches/RefactorInput/ocw/plotter.py
URL: http://svn.apache.org/viewvc/incubator/climate/branches/RefactorInput/ocw/plotter.py?rev=1508985&r1=1508984&r2=1508985&view=diff
==============================================================================
--- incubator/climate/branches/RefactorInput/ocw/plotter.py (original)
+++ incubator/climate/branches/RefactorInput/ocw/plotter.py Wed Jul 31 18:44:17 2013
@@ -259,44 +259,53 @@ def _best_grid_shape(nplots, oldshape):
         newshape = nrows, ncols
         return newshape
     
-def _fig_size(gridshape):
+def _fig_size(gridshape, aspect=None):
     '''
     Purpose::
         Calculates the figure dimensions from a subplot gridshape
         
     Input::
         gridshape - Tuple denoting the subplot gridshape
+        aspect - Float denoting approximate aspect ratio of each subplot 
+                 (width / height). Default is 8.5 / 5.5
         
     Output::
         width - float for width of the figure in inches
         height - float for height of the figure in inches
     '''
+    if aspect is None:
+        aspect = 8.5 / 5.5
+
+    # Default figure size is 8.5" x 5.5" if nrows == ncols
+    # and then adjusted by given aspect ratio
     nrows, ncols = gridshape
-    
-    # Assuming base dimensions of 8.5" x 5.5". May change this later to be
-    # user defined.
     if nrows >= ncols:
-        width, height = 8.5, 5.5 * nrows / ncols
+        # If more rows keep width constant
+        width, height = (aspect * 5.5), 5.5 * (nrows / ncols)
     else:
-        width, height = 8.5 * ncols / nrows, 5.5
+        # If more columns keep height constant
+        width, height = (aspect * 5.5) * (ncols / nrows), 5.5
         
     return width, height
     
-def draw_taylor_diagram(datasets, names, refname, fname, fmt='png', 
-                        gridshape=(1,1), ptitle='', pos='center right', 
-                        frameon=True, radmax=1.5):
+def draw_taylor_diagram(results, names, refname, fname, fmt='png', 
+                        gridshape=(1,1), ptitle='', subtitles=None, 
+                        pos='upper right', frameon=True, radmax=1.5):
     '''
     Purpose::
         Draws a Taylor diagram
         
     Input::
         dataset - an Nx2 array containing normalized standard deviations,
-               correlation coefficients, and names of evaluation datasets
+               correlation coefficients, and names of evaluation results
         names - list of names for each evaluated dataset
         refname - The name of the reference dataset
         fname  - a string specifying the filename of the plot
         fmt  - an optional string specifying the filetype, default is .png
+        gridshape - optional tuple denoting the desired grid shape (nrows, ncols) for arranging
+                    the subplots. 
         ptitle - an optional string specifying the plot title
+        subtitles - an optional list of strings specifying the title for each subplot
         pos - an optional string or tuple of float for determining 
                     the position of the legend
         frameon - an optional boolean that determines whether to draw a frame
@@ -305,26 +314,24 @@ def draw_taylor_diagram(datasets, names,
                  standard deviation.
     '''
     # Handle the single plot case. 
-    if datasets.ndim == 2:
-        datasets = datasets.reshape(1, *datasets.shape)
+    if results.ndim == 2:
+        results = results.reshape(1, *results.shape)
 
     # Make sure gridshape is compatible with input data
-    nplots = datasets.shape[0]
+    nplots = results.shape[0]
     gridshape = _best_grid_shape(nplots, gridshape)
     
     # Set up the figure
     fig = plt.figure()
-    width, height = gridshape
-    width *= 6
-    height *= 6
-    fig.set_size_inches((width, height))
+    fig.set_size_inches((8.5, 11))
     fig.dpi = 300
-    for i, data in enumerate(datasets):
+    for i, data in enumerate(results):
         rect = gridshape + (i + 1,)
         dia = TaylorDiagram(1, fig=fig, rect=rect, label=refname, radmax=radmax)
         for i, (stddev, corrcoef) in enumerate(data):
             dia.add_sample(stddev, corrcoef, marker='$%d$' % (i + 1), ms=6, 
                            label=names[i])
+            dia._ax.set_title(subtitles[i])
     
     # Add legend
     legend = fig.legend(dia.samplePoints, 
@@ -434,15 +441,15 @@ def draw_subregions(subregions, lats, lo
     fig.savefig('%s.%s' %(fname, fmt), bbox_inches='tight', dpi=fig.dpi)
     fig.clf()
 
-def draw_time_series(datasets, times, labels, fname, fmt='png', gridshape=(1, 1), 
+def draw_time_series(results, times, labels, fname, fmt='png', gridshape=(1, 1), 
                      xlabel='', ylabel='', ptitle='', subtitles=None, 
-                     label_month=False, yscale='linear'):
+                     label_month=False, yscale='linear', aspect=None):
     '''
     Purpose::
         Function to draw a time series plot
      
     Input:: 
-        datasets - a 3d array of time series
+        results - a 3d array of time series
         times - a list of python datetime objects
         labels - a list of strings with the names of each set of data
         fname - a string specifying the filename of the plot
@@ -456,13 +463,15 @@ def draw_time_series(datasets, times, la
         label_month - optional bool to toggle drawing month labels
         yscale - optional string for setting the y-axis scale, 'linear' for linear
                  and 'log' for log base 10.
+        aspect - Float denoting approximate aspect ratio of each subplot 
+                 (width / height). Default is 8.5 / 5.5
     '''
     # Handle the single plot case. 
-    if datasets.ndim == 2:
-        datasets = datasets.reshape(1, *datasets.shape)
+    if results.ndim == 2:
+        results = results.reshape(1, *results.shape)
 
     # Make sure gridshape is compatible with input data
-    nplots = datasets.shape[0]
+    nplots = results.shape[0]
     gridshape = _best_grid_shape(nplots, gridshape)
         
     # Set up the figure
@@ -488,7 +497,7 @@ def draw_time_series(datasets, times, la
     
     # Make the plots
     for i, ax in enumerate(grid):
-        data = datasets[i]
+        data = results[i]
         if label_month:
             xfmt = mpl.dates.DateFormatter('%b')
             xloc = mpl.dates.MonthLocator()
@@ -554,7 +563,7 @@ def draw_time_series(datasets, times, la
 def draw_contour_map(dataset, lats, lons, fname, fmt='png', gridshape=(1, 1),
                      clabel='', ptitle='', subtitles=None, cmap=None, 
                      clevs=None, nlevs=10, parallels=None, meridians=None,
-                     extend='neither'):
+                     extend='neither', aspect=8.5/2.5):
     '''
     Purpose::
         Create a multiple panel contour map plot.
@@ -695,16 +704,16 @@ def draw_contour_map(dataset, lats, lons
     fig.savefig('%s.%s' %(fname, fmt), bbox_inches='tight', dpi=fig.dpi)
     fig.clf()
 
-def draw_portrait_diagram(datasets, rowlabels, collabels, fname, fmt='png', 
+def draw_portrait_diagram(results, rowlabels, collabels, fname, fmt='png', 
                           gridshape=(1, 1), xlabel='', ylabel='', clabel='', 
                           ptitle='', subtitles=None, cmap=None, clevs=None, 
-                          nlevs=10, extend='neither'):
+                          nlevs=10, extend='neither', aspect=None):
     '''
     Purpose::
         Makes a portrait diagram plot.
         
     Input::
-        datasets - 3d array of the field to be plotted. The second dimension 
+        results - 3d array of the field to be plotted. The second dimension 
                   should correspond to the number of rows in the diagram and the
                   third should correspond to the number of columns.
         rowlabels - a list of strings denoting labels for each row
@@ -725,20 +734,21 @@ def draw_portrait_diagram(datasets, rowl
         extend - an optional string to toggle whether to place arrows at the colorbar
              boundaries. Default is 'neither', but can also be 'min', 'max', or
              'both'. Will be automatically set to 'both' if clevs is None.     
-        
+        aspect - Float denoting approximate aspect ratio of each subplot 
+                 (width / height). Default is 8.5 / 5.5
     '''  
     # Handle the single plot case.
-    if datasets.ndim == 2:
-        datasets = datasets.reshape(1, *datasets.shape)
+    if results.ndim == 2:
+        results = results.reshape(1, *results.shape)
     
-    nplots = datasets.shape[0]
+    nplots = results.shape[0]
     
     # Make sure gridshape is compatible with input data
     gridshape = _best_grid_shape(nplots, gridshape)
     
     # Row and Column labels must be consistent with the shape of
     # the input data too
-    prows, pcols = datasets.shape[1:]
+    prows, pcols = results.shape[1:]
     if len(rowlabels) != prows or len(collabels) != pcols:
         raise ValueError('rowlabels and collabels must have %d and %d elements respectively'
%(prows, pcols))
      
@@ -767,7 +777,7 @@ def draw_portrait_diagram(datasets, rowl
     if clevs is None:
         # Cut off the tails of the distribution
         # for more representative colorbar levels
-        clevs = _nice_intervals(datasets, nlevs)
+        clevs = _nice_intervals(results, nlevs)
         extend = 'both'
         
     if cmap is None:
@@ -777,7 +787,7 @@ def draw_portrait_diagram(datasets, rowl
     
     # Do the plotting
     for i, ax in enumerate(grid):
-        data = datasets[i]
+        data = results[i]
         cs = ax.matshow(data, cmap=cmap, aspect='auto', origin='lower', norm=norm)
         
         # Add grid lines
@@ -824,4 +834,4 @@ def draw_portrait_diagram(datasets, rowl
     
     # Save the figure
     fig.savefig('%s.%s' %(fname, fmt), bbox_inches='tight', dpi=fig.dpi)
-    fig.clf()
+    fig.clf()
\ No newline at end of file



Mime
View raw message