~ubuntu-branches/ubuntu/lucid/python-scipy/lucid

« back to all changes in this revision

Viewing changes to Lib/xplt/colorbar.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-01-07 14:12:12 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20070107141212-mm0ebkh5b37hcpzn
* Remove build dependency on python-numpy-dev.
* python-scipy: Depend on python-numpy instead of python-numpy-dev.
* Package builds on other archs than i386. Closes: #402783.

Show diffs side-by-side

added added

removed removed

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