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

Skip to content

Commit 7c26f78

Browse files
committed
Bilinear interp in NonUniformImage; slightly modified patch by Gregory Lielens
Includes a bugfix in _image.pcolor2. Replaced pylab_examples/pcolor_nonuniform.py with the more aptly-named image_nonuniform.py, modified it to show both interpolation types, and added it to backend_driver.py. svn path=/trunk/matplotlib/; revision=6057
1 parent c301cf1 commit 7c26f78

5 files changed

Lines changed: 352 additions & 168 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'''
2+
This illustrates the NonUniformImage class, which still needs
3+
an axes method interface; either a separate interface, or a
4+
generalization of imshow.
5+
'''
6+
7+
from matplotlib.pyplot import figure, show
8+
import numpy as np
9+
from matplotlib.image import NonUniformImage
10+
11+
interp='nearest'
12+
13+
x = np.linspace(-4, 4, 9)
14+
x2 = x**3
15+
y = np.linspace(-4, 4, 9)
16+
print 'Size %d points' % (len(x) * len(y))
17+
z = np.sqrt(x[np.newaxis,:]**2 + y[:,np.newaxis]**2)
18+
19+
fig = figure()
20+
fig.suptitle('NonUniformImage class')
21+
ax = fig.add_subplot(221)
22+
im = NonUniformImage(ax, interpolation=interp, extent=(-4,4,-4,4))
23+
im.set_data(x, y, z)
24+
ax.images.append(im)
25+
ax.set_xlim(-4,4)
26+
ax.set_ylim(-4,4)
27+
ax.set_title(interp)
28+
29+
ax = fig.add_subplot(222)
30+
im = NonUniformImage(ax, interpolation=interp, extent=(-64,64,-4,4))
31+
im.set_data(x2, y, z)
32+
ax.images.append(im)
33+
ax.set_xlim(-64,64)
34+
ax.set_ylim(-4,4)
35+
ax.set_title(interp)
36+
37+
interp = 'bilinear'
38+
39+
ax = fig.add_subplot(223)
40+
im = NonUniformImage(ax, interpolation=interp, extent=(-4,4,-4,4))
41+
im.set_data(x, y, z)
42+
ax.images.append(im)
43+
ax.set_xlim(-4,4)
44+
ax.set_ylim(-4,4)
45+
ax.set_title(interp)
46+
47+
ax = fig.add_subplot(224)
48+
im = NonUniformImage(ax, interpolation=interp, extent=(-64,64,-4,4))
49+
im.set_data(x2, y, z)
50+
ax.images.append(im)
51+
ax.set_xlim(-64,64)
52+
ax.set_ylim(-4,4)
53+
ax.set_title(interp)
54+
55+
show()

examples/pylab_examples/pcolor_nonuniform.py

Lines changed: 0 additions & 26 deletions
This file was deleted.

examples/tests/backend_driver.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
'image_demo2.py',
6767
'image_masked.py',
6868
'image_origin.py',
69+
'image_nonuniform.py',
6970
'invert_axes.py',
7071
'layer_images.py',
7172
'legend_auto.py',

lib/matplotlib/image.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -392,13 +392,14 @@ class NonUniformImage(AxesImage):
392392
def __init__(self, ax,
393393
**kwargs
394394
):
395+
interp = kwargs.pop('interpolation', 'nearest')
395396
AxesImage.__init__(self, ax,
396397
**kwargs)
398+
AxesImage.set_interpolation(self, interp)
397399

398400
def make_image(self, magnification=1.0):
399401
if self._A is None:
400402
raise RuntimeError('You must first set the image array')
401-
402403
x0, y0, v_width, v_height = self.axes.viewLim.bounds
403404
l, b, r, t = self.axes.bbox.extents
404405
width = (round(r) + 0.5) - (round(l) - 0.5)
@@ -407,10 +408,13 @@ def make_image(self, magnification=1.0):
407408
height *= magnification
408409
im = _image.pcolor(self._Ax, self._Ay, self._A,
409410
height, width,
410-
(x0, x0+v_width, y0, y0+v_height))
411+
(x0, x0+v_width, y0, y0+v_height),
412+
self._interpd[self._interpolation])
413+
411414
fc = self.axes.patch.get_facecolor()
412415
bg = mcolors.colorConverter.to_rgba(fc, 0)
413416
im.set_bg(*bg)
417+
im.is_grayscale = self.is_grayscale
414418
return im
415419

416420
def set_data(self, x, y, A):
@@ -430,9 +434,11 @@ def set_data(self, x, y, A):
430434
if len(A.shape) == 2:
431435
if A.dtype != np.uint8:
432436
A = (self.cmap(self.norm(A))*255).astype(np.uint8)
437+
self.is_grayscale = self.cmap.is_gray()
433438
else:
434439
A = np.repeat(A[:,:,np.newaxis], 4, 2)
435440
A[:,:,3] = 255
441+
self.is_grayscale = True
436442
else:
437443
if A.dtype != np.uint8:
438444
A = (255*A).astype(np.uint8)
@@ -441,6 +447,7 @@ def set_data(self, x, y, A):
441447
B[:,:,0:3] = A
442448
B[:,:,3] = 255
443449
A = B
450+
self.is_grayscale = False
444451
self._A = A
445452
self._Ax = x
446453
self._Ay = y
@@ -450,8 +457,8 @@ def set_array(self, *args):
450457
raise NotImplementedError('Method not supported')
451458

452459
def set_interpolation(self, s):
453-
if s != None and s != 'nearest':
454-
raise NotImplementedError('Only nearest neighbor supported')
460+
if s != None and not s in ('nearest','bilinear'):
461+
raise NotImplementedError('Only nearest neighbor and bilinear interpolations are supported')
455462
AxesImage.set_interpolation(self, s)
456463

457464
def get_extent(self):

0 commit comments

Comments
 (0)