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

Skip to content

Commit 559d552

Browse files
committed
draw_image api to use an arbitrary affine transform
svn path=/trunk/matplotlib/; revision=8037
1 parent 6713440 commit 559d552

2 files changed

Lines changed: 33 additions & 22 deletions

File tree

lib/matplotlib/backends/backend_ps.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -386,14 +386,15 @@ def option_scale_image(self):
386386
"""
387387
return True
388388

389-
def draw_image(self, gc, x, y, im, sx=None, sy=None):
389+
def draw_image(self, gc, x, y, im, dx=None, dy=None, transform=None):
390390
"""
391391
Draw the Image instance into the current axes; x is the
392392
distance in pixels from the left hand side of the canvas and y
393393
is the distance from bottom
394394
395-
bbox is a matplotlib.transforms.BBox instance for clipping, or
396-
None
395+
dx, dy is the width and height of the image. If a transform
396+
(which must be an affine transform) is given, x, y, dx, dy are
397+
interpreted as the coordinate of the transform.
397398
"""
398399

399400
im.flipud_out()
@@ -406,13 +407,22 @@ def draw_image(self, gc, x, y, im, sx=None, sy=None):
406407
imagecmd = "false 3 colorimage"
407408
hexlines = '\n'.join(self._hex_lines(bits))
408409

409-
if sx is None:
410-
sx = 1./self.image_magnification
411-
if sy is None:
412-
sy = 1./self.image_magnification
413-
414-
xscale, yscale = (w*sx, h*sy)
415-
410+
if dx is None:
411+
xscale = w / self.image_magnification
412+
else:
413+
xscale = dx
414+
415+
if dy is None:
416+
yscale = h/self.image_magnification
417+
else:
418+
yscale = dy
419+
420+
421+
if transform is None:
422+
matrix = "1 0 0 1 0 0"
423+
else:
424+
matrix = " ".join(map(str, transform.to_values()))
425+
416426
figh = self.height*72
417427
#print 'values', origin, flipud, figh, h, y
418428

@@ -431,6 +441,7 @@ def draw_image(self, gc, x, y, im, sx=None, sy=None):
431441
#y = figh-(y+h)
432442
ps = """gsave
433443
%(clip)s
444+
[%(matrix)s] concat
434445
%(x)s %(y)s translate
435446
%(xscale)s %(yscale)s scale
436447
/DataString %(w)s string def

lib/matplotlib/image.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def _get_unsampled_image(self, A, image_extents, viewlim):
144144
sy = dyintv/viewlim.height
145145
numrows, numcols = A.shape[:2]
146146
if sx > 2:
147-
x0 = (viewim.x0-xmin)/dxintv * numcols
147+
x0 = (viewlim.x0-xmin)/dxintv * numcols
148148
ix0 = max(0, int(x0 - self._filterrad))
149149
x1 = (viewlim.x1-xmin)/dxintv * numcols
150150
ix1 = min(numcols, int(x1 + self._filterrad))
@@ -170,7 +170,7 @@ def _get_unsampled_image(self, A, image_extents, viewlim):
170170
ymin = ymin_old + iy0*dyintv/numrows
171171
ymax = ymin_old + iy1*dyintv/numrows
172172
dyintv = ymax - ymin
173-
sy = dyintv/self.axes.viewLim.height
173+
sy = dyintv/viewlim.height
174174
else:
175175
yslice = slice(0, numrows)
176176

@@ -203,7 +203,7 @@ def _get_unsampled_image(self, A, image_extents, viewlim):
203203

204204
return im, xmin, ymin, dxintv, dyintv, sx, sy
205205

206-
206+
207207
def _draw_unsampled_image(self, renderer, gc):
208208
"""
209209
draw unsampled image. The renderer should support a draw_image method
@@ -213,10 +213,6 @@ def _draw_unsampled_image(self, renderer, gc):
213213
self._get_unsampled_image(self._A, self.get_extent(), self.axes.viewLim)
214214

215215
if im is None: return # I'm not if this check is required. -JJL
216-
217-
transData = self.axes.transData
218-
xx1, yy1 = transData.transform_point((xmin, ymin))
219-
xx2, yy2 = transData.transform_point((xmin+dxintv, ymin+dyintv))
220216

221217
fc = self.axes.patch.get_facecolor()
222218
bg = mcolors.colorConverter.to_rgba(fc, 0)
@@ -228,19 +224,23 @@ def _draw_unsampled_image(self, renderer, gc):
228224

229225
im.resize(numcols, numrows) # just to create im.bufOut that is required by backends. There may be better solution -JJL
230226

231-
sx = (xx2-xx1)/numcols
232-
sy = (yy2-yy1)/numrows
233227
im._url = self.get_url()
234-
renderer.draw_image(gc, xx1, yy1, im, sx, sy)
235228

236-
229+
trans = self.get_transform() #axes.transData
230+
xx1, yy1 = trans.transform_non_affine((xmin, ymin))
231+
xx2, yy2 = trans.transform_non_affine((xmin+dxintv, ymin+dyintv))
232+
233+
renderer.draw_image(gc, xx1, yy1, im, xx2-xx1, yy2-yy1,
234+
trans.get_affine())
235+
236+
237237
def _check_unsampled_image(self, renderer):
238238
"""
239239
return True if the image is better to be drawn unsampled.
240240
The derived class needs to override it.
241241
"""
242242
return False
243-
243+
244244
@allow_rasterization
245245
def draw(self, renderer, *args, **kwargs):
246246
if not self.get_visible(): return

0 commit comments

Comments
 (0)