Skip to content

Commit 1ea328c

Browse files
committed
Image in non-linear coordinates correctly have their coners at its extents
svn path=/trunk/matplotlib/; revision=8386
1 parent e24bb60 commit 1ea328c

File tree

1 file changed

+34
-13
lines changed

1 file changed

+34
-13
lines changed

lib/matplotlib/image.py

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def make_image(self, magnification=1.0):
135135
raise RuntimeError('The make_image method must be overridden.')
136136

137137

138-
def _get_unsampled_image(self, A, image_extents, viewlim):
138+
def _get_unsampled_image(self, A, image_extents, viewlim, noslice=False):
139139
"""
140140
convert numpy array A with given extents ([x1, x2, y1, y2] in
141141
data coordinate) into the Image, given the vielim (should be a
@@ -150,7 +150,7 @@ def _get_unsampled_image(self, A, image_extents, viewlim):
150150
sx = dxintv/viewlim.width
151151
sy = dyintv/viewlim.height
152152
numrows, numcols = A.shape[:2]
153-
if sx > 2:
153+
if noslice is False and sx > 2:
154154
x0 = (viewlim.x0-xmin)/dxintv * numcols
155155
ix0 = max(0, int(x0 - self._filterrad))
156156
x1 = (viewlim.x1-xmin)/dxintv * numcols
@@ -164,7 +164,7 @@ def _get_unsampled_image(self, A, image_extents, viewlim):
164164
else:
165165
xslice = slice(0, numcols)
166166

167-
if sy > 2:
167+
if noslice is False and sy > 2:
168168
y0 = (viewlim.y0-ymin)/dyintv * numrows
169169
iy0 = max(0, int(y0 - self._filterrad))
170170
y1 = (viewlim.y1-ymin)/dyintv * numrows
@@ -246,8 +246,11 @@ def _draw_unsampled_image(self, renderer, gc):
246246
draw unsampled image. The renderer should support a draw_image method
247247
with scale parameter.
248248
"""
249+
250+
249251
im, xmin, ymin, dxintv, dyintv, sx, sy = \
250-
self._get_unsampled_image(self._A, self.get_extent(), self.axes.viewLim)
252+
self._get_unsampled_image(self._A, self.get_extent(),
253+
self.axes.viewLim, noslice=True)
251254

252255
if im is None: return # I'm not if this check is required. -JJL
253256

@@ -264,18 +267,23 @@ def _draw_unsampled_image(self, renderer, gc):
264267
im._url = self.get_url()
265268

266269
trans = self.get_transform() #axes.transData
267-
xy = trans.transform_non_affine([(xmin, ymin),
268-
(xmin+dxintv, ymin+dyintv)])
270+
xy = trans.transform_non_affine(np.array([(xmin, ymin),
271+
(xmin+dxintv, ymin+dyintv)]))
269272
xx1, yy1 = xy[0]
270273
xx2, yy2 = xy[1]
271274

272275
if self._image_skew_coordinate:
273276
# skew the image when required.
277+
x_llc, x_trc, y_llc, y_trc = self.get_extent()
274278
x_lrc, y_lrc = self._image_skew_coordinate
275-
xy = trans.transform_non_affine([(x_lrc, y_lrc)])
276-
xx3, yy3 = xy[0]
277-
278-
tr_rotate_skew = self._get_rotate_and_skew_transform(xx1, yy1, xx2, yy2, xx3, yy3)
279+
xy = trans.transform_non_affine(np.array([(x_llc, y_llc),
280+
(x_trc, y_trc),
281+
(x_lrc, y_lrc)]))
282+
_xx1, _yy1 = xy[0]
283+
_xx2, _yy2 = xy[1]
284+
_xx3, _yy3 = xy[2]
285+
286+
tr_rotate_skew = self._get_rotate_and_skew_transform(_xx1, _yy1, _xx2, _yy2, _xx3, _yy3)
279287
tr = tr_rotate_skew+trans.get_affine()
280288
else:
281289
tr = trans.get_affine()
@@ -509,8 +517,21 @@ def make_image(self, magnification=1.0):
509517
if self._A is None:
510518
raise RuntimeError('You must first set the image array or the image attribute')
511519

520+
# image is created in the canvas coordinate.
521+
x1, x2, y1, y2 = self.get_extent()
522+
trans = self.get_transform()
523+
xy = trans.transform(np.array([(x1, y1),
524+
(x2, y2),
525+
]))
526+
_x1, _y1 = xy[0]
527+
_x2, _y2 = xy[1]
528+
529+
transformed_viewLim = mtransforms.TransformedBbox(self.axes.viewLim,
530+
trans)
531+
512532
im, xmin, ymin, dxintv, dyintv, sx, sy = \
513-
self._get_unsampled_image(self._A, self.get_extent(), self.axes.viewLim)
533+
self._get_unsampled_image(self._A, [_x1, _x2, _y1, _y2],
534+
transformed_viewLim)
514535

515536
fc = self.axes.patch.get_facecolor()
516537
bg = mcolors.colorConverter.to_rgba(fc, 0)
@@ -526,8 +547,8 @@ def make_image(self, magnification=1.0):
526547
im.set_resample(self._resample)
527548

528549
# the viewport translation
529-
tx = (xmin-self.axes.viewLim.x0)/dxintv * numcols
530-
ty = (ymin-self.axes.viewLim.y0)/dyintv * numrows
550+
tx = (xmin-transformed_viewLim.x0)/dxintv * numcols
551+
ty = (ymin-transformed_viewLim.y0)/dyintv * numrows
531552

532553
l, b, r, t = self.axes.bbox.extents
533554
widthDisplay = (round(r*magnification) + 0.5) - (round(l*magnification) - 0.5)

0 commit comments

Comments
 (0)