~ubuntu-branches/ubuntu/saucy/python-scipy/saucy

« back to all changes in this revision

Viewing changes to Lib/sandbox/xplt/colorbar.py~

  • Committer: Bazaar Package Importer
  • Author(s): Ondrej Certik
  • Date: 2008-06-16 22:58:01 UTC
  • mfrom: (2.1.24 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080616225801-irdhrpcwiocfbcmt
Tags: 0.6.0-12
* The description updated to match the current SciPy (Closes: #489149).
* Standards-Version bumped to 3.8.0 (no action needed)
* Build-Depends: netcdf-dev changed to libnetcdf-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
## Automatically adapted for scipy Oct 31, 2005 by
2
 
 
3
 
# Copyright (c) 1996, 1997, The Regents of the University of California.
4
 
# All rights reserved.  See Legal.htm for full text and disclaimer.
5
 
 
6
 
# ed williams' colorbar stuff
7
 
from scipy import *
8
 
from numpy.core.umath import *
9
 
from numpy import *
10
 
from gist import *
11
 
from slice3 import *
12
 
 
13
 
def nice_levels (z, n = 8) :
14
 
    """nice_levels(z, n = 8) finds approximately n "nice values"
15
 
    between min(z) and max(z) for axis labels. n defaults to eight.
16
 
    """
17
 
    zmax = max (ravel (z))
18
 
    zmin = min (ravel (z))
19
 
    finest = abs (zmax - zmin) / float (n)
20
 
    # blows up on zmin=zmax
21
 
    unit = 10. ** floor (log10 (finest))
22
 
    finest = finest / unit
23
 
    if finest > 5.0 :
24
 
        finest = 10.
25
 
    elif finest > 2. :
26
 
        finest = 5.
27
 
    elif finest > 1. :
28
 
        finest = 2.
29
 
    unit = unit * finest
30
 
    cmin = unit * ceil (zmin / unit)
31
 
    if (abs (cmin - zmin) < 0.01 * unit) :
32
 
        cmin = cmin + unit
33
 
    cmax = unit * floor (zmax / unit)
34
 
    if (abs (cmax - zmax) < 0.01 * unit) :
35
 
        cmax = cmax - unit
36
 
    n = int ( ( (cmax - cmin) / unit + 0.5) + 1)
37
 
    levs = span (cmin, cmax, n)
38
 
    list = nonzero (less (abs (levs), 0.1 * unit))
39
 
    if len (list) > 0 :
40
 
        array_set (levs, list, 0.0)
41
 
    return levs
42
 
 
43
 
def color_bar (minz, maxz, split = 0, ncol = None, ymax=0.85, ymin=0.44, xmin0=0.62, xmax0=0.64, zlabel=None, fontsize=16, font='helvetica', color='black') :
44
 
    """
45
 
    color_bar (minz, maxz) plots a color bar to the right of the plot square
46
 
    labelled by the z values from minz to maxz.
47
 
 
48
 
    plf (z, y, x)
49
 
    color_bar (z (min, min), z (max, max))
50
 
 
51
 
    or
52
 
    plf (z, y, x, cmin = MINZ, cmax = MAXZ)
53
 
    color_bar (MINZ, MAXZ)
54
 
 
55
 
    are typical usage
56
 
    """
57
 
    if ncol is None:
58
 
        ncol = 100 + (1 - split) * 100
59
 
    plsys (0)
60
 
    if type (minz) == type (maxz) == type (1) : # Do not change!!!
61
 
        plotval = reshape (arange (minz, maxz + 1, dtype = 'B'),
62
 
           (maxz + 1 - minz, 1))
63
 
        pli (plotval, xmin0, ymin, xmax0, ymax) # draw bar
64
 
    elif not split :
65
 
        pli (reshape (span (0, 1, ncol), (ncol, 1)),
66
 
           xmin0, ymin, xmax0, ymax) # draw bar
67
 
    else :
68
 
        pli (reshape (split_bytscl (span (0, 1, ncol), 0).astype ('B'), (ncol, 1)),
69
 
           xmin0, ymin, xmax0, ymax) # draw bar
70
 
    pldj (array ( [xmin0, xmin0]), array ( [ymin, ymax]), array ( [xmax0, xmax0]),
71
 
          array ( [ymin, ymax]), color=color)
72
 
    plsys (1)
73
 
    levs = nice_levels (array ( [minz, maxz]))
74
 
    scales = []
75
 
    for i in range (len (levs)) :
76
 
        scales.append ( "% .5g" % levs [i])
77
 
    ys = ymin + (ymax - ymin) * (levs - minz) / (maxz - minz)
78
 
    llev = len (levs)
79
 
    rllev = range (llev)
80
 
    for i in rllev :
81
 
        plt (scales [i], xmax0+0.005, ys [i], color=color)   # labels
82
 
    xmin = zeros (llev, Float)
83
 
    xmax = zeros (llev, Float)
84
 
    xmin [0:llev] = xmin0
85
 
    xmax [0:llev] = xmax0+0.005
86
 
    plsys (0)
87
 
    pldj (xmin, ys, xmax, ys, color=color) # ticks
88
 
    plsys (1)
89
 
    # Write the max and min on bar
90
 
    xmid = (xmin0 + xmax0)/2.0
91
 
    if max (ys) > (ymax-0.01):
92
 
        plt ("% .5g" % maxz, xmid, ymax + 0.020, justify="CA",
93
 
             color=color)
94
 
    else :
95
 
        plt ("% .5g" % maxz, xmid, ymax + 0.005, justify="CA",
96
 
             color=color)
97
 
    plt ("% .5g" % minz, xmid, ymin - 0.0025, justify = "CT",
98
 
         color=color)
99
 
    if zlabel is None:
100
 
        pass
101
 
    elif zlabel != "":
102
 
        ymidpt = (ymax+ymin)/2.0
103
 
        x0 = xmax0 + 0.04
104
 
        plt(zlabel, x0, ymidpt, color=color,
105
 
               font=font, justify="CB", height=fontsize, orient=3)