Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ab36d67

Browse files
committed
added colormap for contour - set region=1
svn path=/trunk/matplotlib/; revision=878
1 parent c04a6f1 commit ab36d67

13 files changed

Lines changed: 115 additions & 56 deletions

File tree

CHANGELOG

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
New entries should be added at the top
22

3+
2005-01-20 Made some changes to the contour routine - particularly
4+
region=1 seems t fix a lot of the zigzag strangeness.
5+
Added colormaps as default for contour - JDH
6+
37
2005-01-19 Restored builtin names which were overridden (min, max,
4-
abs, round, and sum) in pylab. This is a potentially significant
5-
change for those who were relying on an array version of those
6-
functions that previously overrode builtin function names. - ADS
8+
abs, round, and sum) in pylab. This is a potentially
9+
significant change for those who were relying on an array
10+
version of those functions that previously overrode builtin
11+
function names. - ADS
712

813
2005-01-18 Added accents to mathtext: \hat, \breve, \grave, \bar,
9-
\acute, \tilde, \vec, \dot, \ddot. All of them have the same
10-
syntax, eg to make an overbar you do \bar{o} or to make an o umlaut
11-
you do \ddot{o}. The shortcuts are also provided, eg: \"o \'e \`e
12-
\~n \.x \^y - JDH
14+
\acute, \tilde, \vec, \dot, \ddot. All of them have the
15+
same syntax, eg to make an overbar you do \bar{o} or to
16+
make an o umlaut you do \ddot{o}. The shortcuts are also
17+
provided, eg: \"o \'e \`e \~n \.x \^y - JDH
1318

1419
2005-01-18 Plugged image resize memory leaks - JDH
1520

@@ -25,7 +30,7 @@ New entries should be added at the top
2530

2631
2005-01-15 Support arbitrary colorbar axes and horizontal colorbars - JDH
2732

28-
2005-01-15 Fixed colormar number of colors bug so that the colorbar
33+
2005-01-15 Fixed colormap number of colors bug so that the colorbar
2934
has the same discretization as the image - JDH
3035

3136
2005-01-15 Added Nadia's x,y contour fix - JDH

TODO

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,11 +630,14 @@ ZeroDivisionError: SeparableTransformation::eval_scalars yin interval is zero; c
630630

631631
-- more flexible tick placement.
632632

633-
-- fix horizontal colorbar axes resize if cax is None
633+
-- DONE fix horizontal colorbar axes resize if cax is None
634634

635635
-- mathtext layout bug
636636

637637
from pylab import *
638638
plot(range(10))
639639
title(r'$This\ is\ a\ test$')
640640
show()
641+
642+
643+
-- fix contour mappable to work under changes in clim, cmap

boilerplate.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def %(func)s(*args, **kwargs):
5151
'bar',
5252
'barh',
5353
'cohere',
54-
'contour',
54+
'contour',
5555
'csd',
5656
'errorbar',
5757
'fill',
@@ -85,11 +85,13 @@ def %(func)s(*args, **kwargs):
8585
)
8686

8787
cmappable = {
88+
'contour' : 'if ret[1].mappable is not None: gci._current = ret[1].mappable',
8889
'scatter' : 'gci._current = ret',
8990
'pcolor' : 'gci._current = ret',
9091
'imshow' : 'gci._current = ret',
9192
'spy2' : 'gci._current = ret',
9293
'specgram' : 'gci._current = ret[-1]',
94+
9395
}
9496

9597

examples/accented_text.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
plot(range(10))
1414

1515
title(r'$\ddot{o}\acute{e}\grave{e}\hat{O}\breve{i}\bar{A}\tilde{n}\vec{q}$', fontsize=20)
16-
# shorthad is also supported
17-
xlabel(r"""$\"o\'e\`e\~n\.x\^y$""", fontsize=20)
16+
# shorthand is also supported and curly's are optional
17+
xlabel(r"""$\"o\ddot o \'e\`e\~n\.x\^y$""", fontsize=20)
1818

1919

2020
show()

examples/contour_demo.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
99

1010
# difference of Gaussians
11-
#im = imshow(Z2-Z1, interpolation='bilinear', origin='lower')
12-
axis('off')
11+
#im = imshow(Z2-Z1, interpolation='bilinear', origin='lower', cmap=cm.bone)
12+
#axis('off')
13+
# see contour_demo2.py for a colorbar example
1314
levels, colls = contour(Z2-Z1, levels=6,
1415
linewidths=arange(.5, 4, .5),
1516
colors=('r', 'green', 'blue', (1,1,0), '#afeeee', 0.5),
1617
origin='lower')
1718
legend(loc='lower left')
18-
#colorbar()
1919
savefig('test')
2020
show()
2121

examples/contour_demo2.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env python
2+
from pylab import *
3+
4+
delta = 0.025
5+
x = y = arange(-3.0, 3.0, delta)
6+
X, Y = meshgrid(x, y)
7+
Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
8+
Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
9+
10+
# difference of Gaussians
11+
im = imshow(Z2-Z1, interpolation='bilinear', cmap=cm.gray)
12+
levels, colls = contour(Z2-Z1, linewidths=2)
13+
colorbar()
14+
savefig('test')
15+
show()
16+

lib/matplotlib/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,6 @@ def ge(self, level):
268268

269269
verbose=Verbose('error')
270270

271-
272-
273271
def get_home():
274272
"""
275273
return the users HOME dir across platforms or None.

lib/matplotlib/axes.py

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from 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

1212
from numerix import max as nxmax
@@ -20,7 +20,7 @@
2020
from collections import RegularPolyCollection, PolyCollection, LineCollection
2121
from colors import colorConverter, normalize, Colormap, LinearSegmentedColormap
2222
import cm
23-
from cm import ColormapJet, Grayscale
23+
from cm import ColormapJet, Grayscale, ScalarMappable
2424
import _image
2525
from ticker import AutoLocator, LogLocator
2626
from 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
948948
linewidths is one of:
949949
@@ -979,8 +979,15 @@ def contour(self, z,
979979
Return value is levels, collections where levels is a list of contour
980980
levels used and collections is a list of
981981
matplotlib.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

lib/matplotlib/cm.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import colors
66
from matplotlib import verbose
77
from matplotlib import rcParams
8+
from matplotlib.numerix import asarray
89
from numerix import nx
910
LUTSIZE = rcParams['image.lut']
1011

@@ -418,8 +419,10 @@ def __init__(self, norm=None, cmap=None):
418419
def set_colorbar(self, im, ax):
419420
'set the colorbar image and axes associated with mappable'
420421
self.colorbar = im, ax
422+
421423
def to_rgba(self, x, alpha=1.0):
422424
# assume normalized rgb, rgba
425+
x = asarray(x)
423426
if len(x.shape)>2: return x
424427
x = self.norm(x)
425428
return self.cmap(x, alpha)

lib/matplotlib/mathtext.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ def _get_info (self, font, sym, fontsize, dpi):
278278
num = 0
279279
verbose.report_error('unrecognized symbol "%s"' % sym)
280280

281+
#print sym, basename, num
281282
cmfont = self.fonts[basename]
282283
cmfont.set_size(fontsize, dpi)
283284
head = cmfont.get_sfnt_table('head')

0 commit comments

Comments
 (0)