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

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

Added some additional options to set the line width of the coastline and contour lines.

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
23
24config = geoplot_config.getConfig()
25
26#setup the logging
27log = logging.getLogger(__name__)
28
29class ContourDrawer(object):
30    """
31    Responsible for knowing how to draw contours onto an axis, the data
32    for the contours is taken form a grid object
33    """
34
35    def __init__(self, numContourLines=10, contourLabelInterval=1, contourLineWidth=0.5):
36        """
37        Constructs a ContourDrawer object             
38        """
39
40        self.numContourLines = numContourLines
41        self.contourLabelInterval = contourLabelInterval
42        self.contourLineWidth = contourLineWidth
43        self._contourFont = config['Fonts']['ContourLines']
44       
45    def draw(self, axes, grid, basemap=None, normalize=None, cmap=None, fontSize='medium'):
46        """
47        Draws contour lines from the grid's data on the axis.
48       
49        @param axes: the axes the grid will be drawn on
50        @type axes: matplotlib.axes
51        @param grid: the grid to be drawn
52        @type grid: geoplot.grid
53        @keyword basemap: the basemap instance to scale the grid values to be
54            drawn on the axis.
55        """
56
57        if cmap == None:
58            cmap = matplotlib.cm.get_cmap()
59            cmap.set_bad("w")
60           
61        #transform the grid data to the correct projection
62        grid_mapUnits = grid.transform(basemap)
63               
64        self._drawContours(axes, cmap, grid_mapUnits, normalize, fontSize)
65       
66    def _drawContours(self, axes, cmap, grid_mapUnits, normalize, fontSize):
67        """
68        Draws the contour lines onto the axes.
69        """
70
71        #check the values aren't all masked
72        valuesFound = not reduce(operator.and_, grid_mapUnits.values.mask.flat)
73       
74        if valuesFound == True:
75
76            if normalize == None :
77                normalize = matplotlib.colors.Normalize(0, 1)
78                       
79            levels = N.linspace(normalize.vmin, normalize.vmax, self.numContourLines)
80           
81            extent=(grid_mapUnits.boundsX.min(), grid_mapUnits.boundsX.max(),
82                    grid_mapUnits.boundsY.min(), grid_mapUnits.boundsY.max())
83           
84            CS = axes.contour(grid_mapUnits.midpointsX, 
85                              grid_mapUnits.midpointsY, 
86                              grid_mapUnits.values,
87                              levels,
88                 cmap=cmap,
89                 origin='lower',
90                 linewidths=self.contourLineWidth,
91                 norm=normalize,
92                 extend='neither',
93                 )
94           
95            labelDictionary = self._contourFont.getDict(fontSize)
96           
97            axes.clabel(CS,levels[0::self.contourLabelInterval], fontsize=labelDictionary['size'])
98           
99            axes.set_aspect('auto')
100           
Note: See TracBrowser for help on using the repository browser.