source: qesdi/geoplot/trunk/lib/geoplot/contour_drawer.py @ 6103

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/qesdi/geoplot/trunk/lib/geoplot/contour_drawer.py@6103
Revision 6103, 3.5 KB checked in by pnorton, 11 years ago (diff)

Trying to get the different colour bars and plot options to work with the different legend types.

Line 
1"""
2grid_drawer.py
3============
4
5A GridDrawere knows how to draw a lat-lon grid onto a axes so that it will match
6with a basemap drawing on the same axis.
7
8"""
9#python modules
10import logging
11import operator
12from math import *
13
14#third party modules
15import matplotlib.colors
16import matplotlib.cm
17import matplotlib.collections
18import numpy as N
19
20#internal modules
21from geoplot.array_util import *
22import geoplot.config as geoplot_config
23from geoplot.colour_scheme import COLOUR_SCHEME_SCALE
24
25
26config = geoplot_config.getConfig()
27
28#setup the logging
29log = logging.getLogger(__name__)
30
31class ContourDrawer(object):
32    """
33    Responsible for knowing how to draw contours onto an axis, the data
34    for the contours is taken form a grid object
35    """
36
37    def __init__(self, numContourLines=10, contourLabelInterval=1, contourLineWidth=0.5):
38        """
39        Constructs a ContourDrawer object             
40        """
41
42        self.numContourLines = numContourLines
43        self.contourLabelInterval = contourLabelInterval
44        self.contourLineWidth = contourLineWidth
45        self._contourFont = config['Fonts']['ContourLines']
46       
47    def draw(self, axes, grid, scheme, basemap=None, fontSize='medium'):
48        """
49        Draws contour lines from the grid's data on the axis.
50       
51        @param axes: the axes the grid will be drawn on
52        @type axes: matplotlib.axes
53        @param grid: the grid to be drawn
54        @type grid: geoplot.grid
55        @keyword basemap: the basemap instance to scale the grid values to be
56            drawn on the axis.
57        """
58           
59        #transform the grid data to the correct projection
60        grid_mapUnits = grid.transform(basemap)
61               
62        self._drawContours(axes, scheme, grid_mapUnits, fontSize)
63       
64    def _drawContours(self, axes, scheme, grid_mapUnits, fontSize):
65        """
66        Draws the contour lines onto the axes.
67        """
68
69        #check the values aren't all masked
70        valuesFound = not reduce(operator.and_, grid_mapUnits.values.mask.flat)
71       
72        if valuesFound == True:
73           
74#            if scheme.scale == COLOUR_SCHEME_SCALE.LOG:
75#
76#                #lev_exp = np.arange(np.floor(np.log10(z.min())-1),
77#                #                       np.ceil(np.log10(z.max())+1))
78#                #levs = np.power(10, lev_exp)
79#                lmin = N.log10(scheme.norm.vmin)
80#                lmax = N.log10(scheme.norm.vmax)
81#                levels = N.logspace(lmin, lmax,  self.numContourLines)
82#                labelFormat = "%.1E"
83#            else:
84#                levels = N.linspace(scheme.norm.vmin, scheme.norm.vmax, self.numContourLines)
85#                labelFormat = "%1.3f"
86           
87           
88           
89            CS = axes.contour(grid_mapUnits.midpointsX, 
90                              grid_mapUnits.midpointsY, 
91                              grid_mapUnits.values,
92                              scheme.bounds,
93                 cmap=scheme.colourMap,
94                 origin='lower',
95                 linewidths=self.contourLineWidth,
96                 norm=scheme.norm,
97                 extend='neither',
98                 )
99           
100            labelDictionary = self._contourFont.getDict(fontSize)
101           
102            axes.clabel(CS,
103                        scheme.bounds[0::self.contourLabelInterval], 
104                        fontsize=labelDictionary['size'],
105                        fmt=scheme.labelFormat)
106           
107            axes.set_aspect('auto')
108           
Note: See TracBrowser for help on using the repository browser.