File: colorbar.py

package info (click to toggle)
python-scipy 0.5.2-0.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 33,888 kB
  • ctags: 44,231
  • sloc: ansic: 156,256; cpp: 90,347; python: 89,604; fortran: 73,083; sh: 1,318; objc: 424; makefile: 342
file content (106 lines) | stat: -rw-r--r-- 3,623 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
## Automatically adapted for scipy Oct 31, 2005 by

# Copyright (c) 1996, 1997, The Regents of the University of California.
# All rights reserved.  See Legal.htm for full text and disclaimer.

# ed williams' colorbar stuff
from scipy import *
from numpy.core.umath import *
from numpy import *
from gist import *
from slice3 import *
from numpy.oldnumeric import nonzero

def nice_levels (z, n = 8) :
    """nice_levels(z, n = 8) finds approximately n "nice values"
    between min(z) and max(z) for axis labels. n defaults to eight.
    """
    zmax = max (ravel (z))
    zmin = min (ravel (z))
    finest = abs (zmax - zmin) / float (n)
    # blows up on zmin=zmax
    unit = 10. ** floor (log10 (finest))
    finest = finest / unit
    if finest > 5.0 :
        finest = 10.
    elif finest > 2. :
        finest = 5.
    elif finest > 1. :
        finest = 2.
    unit = unit * finest
    cmin = unit * ceil (zmin / unit)
    if (abs (cmin - zmin) < 0.01 * unit) :
        cmin = cmin + unit
    cmax = unit * floor (zmax / unit)
    if (abs (cmax - zmax) < 0.01 * unit) :
        cmax = cmax - unit
    n = int ( ( (cmax - cmin) / unit + 0.5) + 1)
    levs = span (cmin, cmax, n)
    list = nonzero (less (abs (levs), 0.1 * unit))
    if len (list) > 0 :
        array_set (levs, list, 0.0)
    return levs

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') :
    """
    color_bar (minz, maxz) plots a color bar to the right of the plot square
    labelled by the z values from minz to maxz.

    plf (z, y, x)
    color_bar (z (min, min), z (max, max))

    or
    plf (z, y, x, cmin = MINZ, cmax = MAXZ)
    color_bar (MINZ, MAXZ)

    are typical usage
    """
    if ncol is None:
        ncol = 100 + (1 - split) * 100
    plsys (0)
    if type (minz) == type (maxz) == type (1) : # Do not change!!!
        plotval = reshape (arange (minz, maxz + 1, dtype = 'B'),
           (maxz + 1 - minz, 1))
        pli (plotval, xmin0, ymin, xmax0, ymax) # draw bar
    elif not split :
        pli (reshape (span (0, 1, ncol), (ncol, 1)),
           xmin0, ymin, xmax0, ymax) # draw bar
    else :
        pli (reshape (split_bytscl (span (0, 1, ncol), 0).astype ('B'), (ncol, 1)),
           xmin0, ymin, xmax0, ymax) # draw bar
    pldj (array ( [xmin0, xmin0]), array ( [ymin, ymax]), array ( [xmax0, xmax0]),
          array ( [ymin, ymax]), color=color)
    plsys (1)
    levs = nice_levels (array ( [minz, maxz]))
    scales = []
    for i in range (len (levs)) :
        scales.append ( "% .5g" % levs [i])
    ys = ymin + (ymax - ymin) * (levs - minz) / (maxz - minz)
    llev = len (levs)
    rllev = range (llev)
    for i in rllev :
        plt (scales [i], xmax0+0.005, ys [i], color=color)   # labels
    xmin = zeros (llev, Float)
    xmax = zeros (llev, Float)
    xmin [0:llev] = xmin0
    xmax [0:llev] = xmax0+0.005
    plsys (0)
    pldj (xmin, ys, xmax, ys, color=color) # ticks
    plsys (1)
    # Write the max and min on bar
    xmid = (xmin0 + xmax0)/2.0
    if max (ys) > (ymax-0.01):
        plt ("% .5g" % maxz, xmid, ymax + 0.020, justify="CA",
             color=color)
    else :
        plt ("% .5g" % maxz, xmid, ymax + 0.005, justify="CA",
             color=color)
    plt ("% .5g" % minz, xmid, ymin - 0.0025, justify = "CT",
         color=color)
    if zlabel is None:
        pass
    elif zlabel != "":
        ymidpt = (ymax+ymin)/2.0
        x0 = xmax0 + 0.04
        plt(zlabel, x0, ymidpt, color=color,
               font=font, justify="CB", height=fontsize, orient=3)