66
77from numerix import absolute , arange , array , asarray , ones , divide ,\
88 transpose , log , log10 , Float , Float32 , ravel , zeros ,\
9- Int32 , Float64 , ceil , indices , \
9+ Int16 , Int32 , Int , Float64 , ceil , indices , \
1010 shape , which , where , sqrt
1111
1212from numerix import max as nxmax
2020from collections import RegularPolyCollection , PolyCollection , LineCollection
2121from colors import colorConverter , normalize , Colormap , LinearSegmentedColormap
2222import cm
23- from cm import ColormapJet , Grayscale
23+ from cm import ColormapJet , Grayscale , ScalarMappable
2424import _image
2525from ticker import AutoLocator , LogLocator
2626from ticker import ScalarFormatter , LogFormatter , LogFormatterExponent , LogFormatterMathtext
@@ -918,7 +918,8 @@ def contour(self, z,
918918 linewidths = None ,
919919 alpha = 1.0 ,
920920 fmt = '%1.3f' ,
921- origin = None ):
921+ origin = None ,
922+ cmap = None ):
922923 """\
923924 CONTOUR(z, x = None, y = None, levels = None, colors = None)
924925
@@ -942,8 +943,7 @@ def contour(self, z,
942943 - one string color, e.g. colors = 'r' or colors = 'red', all levels
943944 will be plotted in this color
944945
945- - if colors == None, the default color for lines.color in
946- .matplotlibrc is used.
946+ - if colors == None, the default colormap will be used
947947
948948linewidths is one of:
949949
@@ -979,8 +979,15 @@ def contour(self, z,
979979Return value is levels, collections where levels is a list of contour
980980levels used and collections is a list of
981981matplotlib.collections.LineCollection instances
982+
983+ If cmap is not None, the collections list instance will have the
984+ mappable attribute attached, where mappable is a cm.ScalarMappable
985+ which contains the normalize instance and cmap instance and can be
986+ used for colorbar functionality
982987"""
983988
989+ if colors is not None and cmap is not None :
990+ raise RuntimeError ('Either colors or cmap must be None' )
984991 if origin is None : origin = rcParams ['image.origin' ]
985992
986993 if origin == 'upper' :
@@ -996,10 +1003,9 @@ def contour(self, z,
9961003 else :
9971004 jmax , imax = shape (z )
9981005
999- region = 0
10001006 reg = ones ((jmax ,imax ), Int32 )
10011007 reg [:,0 ]= 0
1002- triangle = zeros ((jmax ,imax ), Int32 )
1008+ triangle = zeros ((jmax ,imax ), Int16 )
10031009
10041010 if x == None and y == None :
10051011 y , x = indices ((jmax ,imax ), 'd' )
@@ -1018,24 +1024,30 @@ def autolev(N):
10181024 lev = list (levels )
10191025 else : lev = autolev (Nlev )
10201026
1027+
1028+
10211029 Nlev = len (lev )
1022- if colors == None :
1023- colors = rcParams ['lines.color' ]
10241030
1025- if is_string_like (colors ):
1026- colors = [colors ] * Nlev
1027- elif iterable (colors ) and len (colors ) < Nlev :
1028- colors = list (colors ) * Nlev
1029- else :
1030- try : gray = float (colors )
1031- except TypeError : pass
1032- else : colors = [gray ] * Nlev
1031+ if colors is not None :
10331032
1033+ if is_string_like (colors ):
1034+ colors = [colors ] * Nlev
1035+ elif iterable (colors ) and len (colors ) < Nlev :
1036+ colors = list (colors ) * Nlev
1037+ else :
1038+ try : gray = float (colors )
1039+ except TypeError : pass
1040+ else : colors = [gray ] * Nlev
10341041
1035- tcolors = []
1036- for c in colors :
1037- tcolors .append ((colorConverter .to_rgba (c , alpha ),))
1042+ tcolors = [(colorConverter .to_rgba (c , alpha ),) for c in colors ]
1043+ mappable = None
1044+ else :
1045+ mappable = ScalarMappable (cmap = cmap )
1046+ mappable .set_array (z )
1047+ mappable .autoscale ()
1048+ tcolors = [ (tuple (rgba ),) for rgba in mappable .to_rgba (lev )]
10381049
1050+
10391051 if linewidths == None :
10401052 tlinewidths = [linewidths ] * Nlev
10411053 else :
@@ -1051,24 +1063,23 @@ def autolev(N):
10511063 levels = []
10521064 collections = []
10531065
1054-
1055-
1066+ region = 1 # FIXME: what should this be?
10561067 for level , color , width in args :
10571068 ntotal , nparts = _contour .GcInit1 (x , y , reg , triangle , region , z , level )
1058- np = zeros ((nparts ,), Int32 )
1069+ np = zeros ((nparts ,), Int )
10591070 xp = zeros ((ntotal , ), Float64 )
10601071 yp = zeros ((ntotal ,), Float64 )
10611072 nlist = _contour .GcTrace (np , xp , yp )
1073+
10621074 col = LineCollection (nlist , colors = color , linewidths = width )
10631075 col .set_label (fmt % level )
10641076 self .add_collection (col )
10651077 levels .append (level )
1066- #col.set_linestyle('dashdot ') # dashed|dotted|solid|dashdot
1078+ #col.set_linestyle('dashed ') # dashed|dotted|solid|dashdot
10671079 #dashes = 0, (4,2,8,1)
10681080 #col.set_linestyle( (dashes,) ) # offset, onoffseq
10691081 collections .append (col )
10701082
1071-
10721083 if x is not None :
10731084 rx = ravel (x )
10741085 self .set_xlim ((min (rx ), max (rx )))
@@ -1081,6 +1092,8 @@ def autolev(N):
10811092 else :
10821093 self .set_ylim ([0 ,jmax ])
10831094
1095+ collections = silent_list ('LineCollection' , collections )
1096+ collections .mappable = mappable
10841097 return levels , collections
10851098
10861099
0 commit comments