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

Skip to content

Commit ecac8a9

Browse files
committed
Numpified colorbar.py
svn path=/trunk/matplotlib/; revision=3397
1 parent c3d6d26 commit ecac8a9

File tree

1 file changed

+68
-66
lines changed

1 file changed

+68
-66
lines changed

lib/matplotlib/colorbar.py

Lines changed: 68 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@
1515
1616
'''
1717

18-
import matplotlib.numerix as nx
19-
from matplotlib.mlab import meshgrid, linspace
20-
from matplotlib.numerix.mlab import amin, amax
21-
from matplotlib import colors, cm, ticker
22-
from matplotlib.cbook import iterable, is_string_like
23-
from matplotlib.transforms import Interval, Value, PBox
24-
from matplotlib.lines import Line2D
25-
from matplotlib.patches import Polygon
26-
from matplotlib import rcParams
27-
from matplotlib.collections import LineCollection
28-
from matplotlib.contour import ContourSet
29-
from matplotlib.axes import Axes
18+
import numpy as npy
19+
import matplotlib as mpl
20+
import matplotlib.colors as colors
21+
import matplotlib.cm as cm
22+
import matplotlib.ticker as ticker
23+
import matplotlib.cbook as cbook
24+
import matplotlib.transforms as transforms
25+
import matplotlib.lines as lines
26+
import matplotlib.patches as patches
27+
import matplotlib.collections as collections
28+
import matplotlib.contour as contour
29+
import matplotlib.axes as axes
3030

3131
make_axes_kw_doc = '''
3232
fraction = 0.15; fraction of original axes to use for colorbar
@@ -144,7 +144,7 @@ def __init__(self, ax, cmap=None,
144144
self.filled = filled
145145
self.solids = None
146146
self.lines = None
147-
if iterable(ticks):
147+
if cbook.iterable(ticks):
148148
self.locator = ticker.FixedLocator(ticks, nbins=len(ticks))
149149
else:
150150
self.locator = ticks # Handle default in _ticker()
@@ -153,7 +153,7 @@ def __init__(self, ax, cmap=None,
153153
self.formatter = ticker.LogFormatter()
154154
else:
155155
self.formatter = ticker.ScalarFormatter()
156-
elif is_string_like(format):
156+
elif cbook.is_string_like(format):
157157
self.formatter = ticker.FormatStrFormatter(format)
158158
else:
159159
self.formatter = format # Assume it is a Formatter
@@ -168,7 +168,7 @@ def draw_all(self):
168168
self._process_values()
169169
self._find_range()
170170
X, Y = self._mesh()
171-
C = self._values[:,nx.NewAxis]
171+
C = self._values[:,npy.newaxis]
172172
self._config_axes(X, Y)
173173
if self.filled:
174174
self._add_solids(X, Y, C)
@@ -181,14 +181,14 @@ def _config_axes(self, X, Y):
181181
ax.set_frame_on(False)
182182
ax.set_navigate(False)
183183
x, y = self._outline(X, Y)
184-
ax.set_xlim(amin(x), amax(x))
185-
ax.set_ylim(amin(y), amax(y))
184+
ax.set_xlim(npy.amin(x), npy.amax(x))
185+
ax.set_ylim(npy.amin(y), npy.amax(y))
186186
ax.update_datalim_numerix(x, y)
187-
self.outline = Line2D(x, y, color=rcParams['axes.edgecolor'],
188-
linewidth=rcParams['axes.linewidth'])
187+
self.outline = lines.Line2D(x, y, color=mpl.rcParams['axes.edgecolor'],
188+
linewidth=mpl.rcParams['axes.linewidth'])
189189
ax.add_artist(self.outline)
190-
c = rcParams['axes.facecolor']
191-
self.patch = Polygon(zip(x,y), edgecolor=c,
190+
c = mpl.rcParams['axes.facecolor']
191+
self.patch = patches.Polygon(zip(x,y), edgecolor=c,
192192
facecolor=c,
193193
linewidth=0.01,
194194
zorder=-1)
@@ -220,10 +220,10 @@ def _outline(self, X, Y):
220220
Return x, y arrays of colorbar bounding polygon,
221221
taking orientation into account.
222222
'''
223-
N = nx.shape(X)[0]
223+
N = X.shape[0]
224224
ii = [0, 1, N-2, N-1, 2*N-1, 2*N-2, N+1, N, 0]
225-
x = nx.take(nx.ravel(nx.transpose(X)), ii)
226-
y = nx.take(nx.ravel(nx.transpose(Y)), ii)
225+
x = npy.take(npy.ravel(npy.transpose(X)), ii)
226+
y = npy.take(npy.ravel(npy.transpose(Y)), ii)
227227
if self.orientation == 'horizontal':
228228
return y,x
229229
return x,y
@@ -232,7 +232,7 @@ def _edges(self, X, Y):
232232
'''
233233
Return the separator line segments; helper for _add_solids.
234234
'''
235-
N = nx.shape(X)[0]
235+
N = X.shape[0]
236236
# Using the non-array form of these line segments is much
237237
# simpler than making them into arrays.
238238
if self.orientation == 'vertical':
@@ -249,17 +249,17 @@ def _add_solids(self, X, Y, C):
249249
if self.orientation == 'vertical':
250250
args = (X, Y, C)
251251
else:
252-
args = (nx.transpose(Y), nx.transpose(X), nx.transpose(C))
252+
args = (npy.transpose(Y), npy.transpose(X), npy.transpose(C))
253253
kw = {'cmap':self.cmap, 'norm':self.norm,
254254
'shading':'flat', 'alpha':self.alpha}
255255
col = self.ax.pcolor(*args, **kw)
256256
#self.add_observer(col) # We should observe, not be observed...
257257
self.solids = col
258258
if self.drawedges:
259-
self.dividers = LineCollection(self._edges(X,Y),
260-
colors=(rcParams['axes.edgecolor'],),
261-
linewidths=(0.5*rcParams['axes.linewidth'],)
262-
)
259+
self.dividers = collections.LineCollection(self._edges(X,Y),
260+
colors=(mpl.rcParams['axes.edgecolor'],),
261+
linewidths=(0.5*mpl.rcParams['axes.linewidth'],)
262+
)
263263
self.ax.add_collection(self.dividers)
264264

265265
def add_lines(self, levels, colors, linewidths):
@@ -270,13 +270,13 @@ def add_lines(self, levels, colors, linewidths):
270270
dummy, y = self._locate(levels)
271271
if len(y) <> N:
272272
raise ValueError("levels are outside colorbar range")
273-
x = nx.array([0.0, 1.0])
274-
X, Y = meshgrid(x,y)
273+
x = npy.array([0.0, 1.0])
274+
X, Y = npy.meshgrid(x,y)
275275
if self.orientation == 'vertical':
276276
xy = [zip(X[i], Y[i]) for i in range(N)]
277277
else:
278278
xy = [zip(Y[i], X[i]) for i in range(N)]
279-
col = LineCollection(xy, linewidths=linewidths)
279+
col = collections.LineCollection(xy, linewidths=linewidths)
280280
self.lines = col
281281
col.set_color(colors)
282282
self.ax.add_collection(col)
@@ -303,14 +303,16 @@ def _ticker(self):
303303
b = self._boundaries[self._inside]
304304
locator = ticker.FixedLocator(b, nbins=10)
305305
if isinstance(self.norm, colors.NoNorm):
306-
intv = Interval(Value(self._values[0]), Value(self._values[-1]))
306+
intv = transforms.Interval(transforms.Value(self._values[0]),
307+
transforms.Value(self._values[-1]))
307308
else:
308-
intv = Interval(Value(self.vmin), Value(self.vmax))
309+
intv = transforms.Interval(transforms.Value(self.vmin),
310+
transforms.Value(self.vmax))
309311
locator.set_view_interval(intv)
310312
locator.set_data_interval(intv)
311313
formatter.set_view_interval(intv)
312314
formatter.set_data_interval(intv)
313-
b = nx.array(locator())
315+
b = npy.array(locator())
314316
b, ticks = self._locate(b)
315317
formatter.set_locs(b)
316318
ticklabels = [formatter(t) for t in b]
@@ -326,28 +328,28 @@ def _process_values(self, b=None):
326328
if b is None:
327329
b = self.boundaries
328330
if b is not None:
329-
self._boundaries = nx.array(b)
331+
self._boundaries = npy.array(b)
330332
if self.values is None:
331333
self._values = 0.5*(self._boundaries[:-1]
332334
+ self._boundaries[1:])
333335
if isinstance(self.norm, colors.NoNorm):
334-
self._values = (self._values + 0.00001).astype(nx.Int16)
336+
self._values = (self._values + 0.00001).astype(npy.int16)
335337
return
336-
self._values = nx.array(self.values)
338+
self._values = npy.array(self.values)
337339
return
338340
if self.values is not None:
339-
self._values = nx.array(self.values)
341+
self._values = npy.array(self.values)
340342
if self.boundaries is None:
341-
b = nx.zeros(len(self.values)+1, 'd')
343+
b = npy.zeros(len(self.values)+1, 'd')
342344
b[1:-1] = 0.5*(self._values[:-1] - self._values[1:])
343345
b[0] = 2.0*b[1] - b[2]
344346
b[-1] = 2.0*b[-2] - b[-3]
345347
self._boundaries = b
346348
return
347-
self._boundaries = nx.array(self.boundaries)
349+
self._boundaries = npy.array(self.boundaries)
348350
return
349351
if isinstance(self.norm, colors.NoNorm):
350-
b = nx.arange(self.norm.vmin, self.norm.vmax + 2) - 0.5
352+
b = npy.arange(self.norm.vmin, self.norm.vmax + 2) - 0.5
351353
else:
352354
b = self.norm.inverse(self._uniform_y(self.cmap.N+1))
353355
self._process_values(b)
@@ -388,19 +390,19 @@ def _uniform_y(self, N):
388390
spaced boundaries, plus ends if required.
389391
'''
390392
if self.extend == 'neither':
391-
y = linspace(0, 1, N)
393+
y = npy.linspace(0, 1, N)
392394
else:
393395
if self.extend == 'both':
394-
y = nx.zeros(N + 2, 'd')
396+
y = npy.zeros(N + 2, 'd')
395397
y[0] = -0.05
396398
y[-1] = 1.05
397399
elif self.extend == 'min':
398-
y = nx.zeros(N + 1, 'd')
400+
y = npy.zeros(N + 1, 'd')
399401
y[0] = -0.05
400402
else:
401-
y = nx.zeros(N + 1, 'd')
403+
y = npy.zeros(N + 1, 'd')
402404
y[-1] = 1.05
403-
y[self._inside] = linspace(0, 1, N)
405+
y[self._inside] = npy.linspace(0, 1, N)
404406
return y
405407

406408
def _proportional_y(self):
@@ -425,13 +427,13 @@ def _mesh(self):
425427
transposition for a horizontal colorbar are done outside
426428
this function.
427429
'''
428-
x = nx.array([0.0, 1.0])
430+
x = npy.array([0.0, 1.0])
429431
if self.spacing == 'uniform':
430432
y = self._uniform_y(self._central_N())
431433
else:
432434
y = self._proportional_y()
433435
self._y = y
434-
X, Y = meshgrid(x,y)
436+
X, Y = npy.meshgrid(x,y)
435437
if self.extend in ('min', 'both'):
436438
X[0,:] = 0.5
437439
if self.extend in ('max', 'both'):
@@ -457,19 +459,19 @@ def _locate(self, x):
457459
# floating point errors.
458460
xn = self.norm(x, clip=False).filled()
459461
in_cond = (xn > -0.001) & (xn < 1.001)
460-
xn = nx.compress(in_cond, xn)
461-
xout = nx.compress(in_cond, x)
462+
xn = npy.compress(in_cond, xn)
463+
xout = npy.compress(in_cond, x)
462464
# The rest is linear interpolation with clipping.
463465
y = self._y
464466
N = len(b)
465-
ii = nx.minimum(nx.searchsorted(b, xn), N-1)
466-
i0 = nx.maximum(ii - 1, 0)
467+
ii = npy.minimum(npy.searchsorted(b, xn), N-1)
468+
i0 = npy.maximum(ii - 1, 0)
467469
#db = b[ii] - b[i0] (does not work with Numeric)
468-
db = nx.take(b, ii) - nx.take(b, i0)
469-
db = nx.where(i0==ii, 1.0, db)
470+
db = npy.take(b, ii) - npy.take(b, i0)
471+
db = npy.where(i0==ii, 1.0, db)
470472
#dy = y[ii] - y[i0]
471-
dy = nx.take(y, ii) - nx.take(y, i0)
472-
z = nx.take(y, i0) + (xn-nx.take(b,i0))*dy/db
473+
dy = npy.take(y, ii) - npy.take(y, i0)
474+
z = npy.take(y, i0) + (xn-npy.take(b,i0))*dy/db
473475
return xout, z
474476

475477
def set_alpha(self, alpha):
@@ -486,7 +488,7 @@ def __init__(self, ax, mappable, **kw):
486488
kw['cmap'] = mappable.cmap
487489
kw['norm'] = mappable.norm
488490
kw['alpha'] = mappable.get_alpha()
489-
if isinstance(mappable, ContourSet):
491+
if isinstance(mappable, contour.ContourSet):
490492
CS = mappable
491493
kw['boundaries'] = CS._levels
492494
kw['values'] = CS.cvalues
@@ -505,7 +507,7 @@ def add_lines(self, CS):
505507
'''
506508
Add the lines from a non-filled ContourSet to the colorbar.
507509
'''
508-
if not isinstance(CS, ContourSet) or CS.filled:
510+
if not isinstance(CS, contour.ContourSet) or CS.filled:
509511
raise ValueError('add_lines is only for a ContourSet of lines')
510512
tcolors = [c[0] for c in CS.tcolors]
511513
tlinewidths = [t[0] for t in CS.tlinewidths]
@@ -530,7 +532,7 @@ def notify(self, mappable):
530532
#if self.vmin != self.norm.vmin or self.vmax != self.norm.vmax:
531533
# self.ax.cla()
532534
# self.draw_all()
533-
if isinstance(self.mappable, ContourSet):
535+
if isinstance(self.mappable, contour.ContourSet):
534536
CS = self.mappable
535537
if not CS.filled:
536538
self.add_lines(CS)
@@ -547,8 +549,8 @@ def make_axes(parent, **kw):
547549
fraction = kw.pop('fraction', 0.15)
548550
shrink = kw.pop('shrink', 1.0)
549551
aspect = kw.pop('aspect', 20)
550-
#pb = PBox(parent.get_position())
551-
pb = PBox(parent.get_position(original=True))
552+
#pb = transforms.PBox(parent.get_position())
553+
pb = transforms.PBox(parent.get_position(original=True))
552554
if orientation == 'vertical':
553555
pad = kw.pop('pad', 0.05)
554556
x1 = 1.0-fraction
@@ -591,14 +593,14 @@ def make_axes(parent, **kw):
591593
generate the axes, not when the axes object is generated first and
592594
then fig.add_axes(ax) is called. I don't understand this. - EF
593595
594-
class ColorbarAxes(Axes):
596+
class ColorbarAxes(axes.Axes):
595597
def __init__(self, parent, **kw):
596598
orientation = kw.setdefault('orientation', 'vertical')
597599
fraction = kw.pop('fraction', 0.15)
598600
shrink = kw.pop('shrink', 1.0)
599601
aspect = kw.pop('aspect', 20)
600602
self.cbkw = kw
601-
pb = PBox(parent.get_position())
603+
pb = transforms.PBox(parent.get_position())
602604
if orientation == 'vertical':
603605
pb1, pbcb = pb.splitx(1.0-fraction)
604606
pbcb.shrink(1.0, shrink).anchor('C')
@@ -613,7 +615,7 @@ def __init__(self, parent, **kw):
613615
parent.set_position(pb1)
614616
parent.set_anchor(panchor)
615617
fig = parent.get_figure()
616-
Axes.__init__(self, fig, pbcb)
618+
axes.Axes.__init__(self, fig, pbcb)
617619
fig.add_axes(self)
618620
self.set_aspect(aspect, anchor=anchor, adjustable='box')
619621

0 commit comments

Comments
 (0)