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

Skip to content

Commit 66dea41

Browse files
committed
_get_unsampled_image now returns a sliced image if possible
svn path=/trunk/matplotlib/; revision=8472
1 parent 02b39b9 commit 66dea41

File tree

1 file changed

+59
-30
lines changed

1 file changed

+59
-30
lines changed

lib/matplotlib/image.py

Lines changed: 59 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
# the image namespace:
2525
from matplotlib._image import *
2626

27-
from matplotlib.transforms import BboxBase
27+
from matplotlib.transforms import BboxBase, Bbox
2828
import matplotlib.transforms as mtransforms
2929

3030
class _AxesImageBase(martist.Artist, cm.ScalarMappable):
@@ -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, noslice=False):
138+
def _get_unsampled_image(self, A, image_extents, viewlim):
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, noslice=False):
150150
sx = dxintv/viewlim.width
151151
sy = dyintv/viewlim.height
152152
numrows, numcols = A.shape[:2]
153-
if noslice is False and sx > 2:
153+
if 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, noslice=False):
164164
else:
165165
xslice = slice(0, numcols)
166166

167-
if noslice is False and sy > 2:
167+
if sy > 2:
168168
y0 = (viewlim.y0-ymin)/dyintv * numrows
169169
iy0 = max(0, int(y0 - self._filterrad))
170170
y1 = (viewlim.y1-ymin)/dyintv * numrows
@@ -248,9 +248,57 @@ def _draw_unsampled_image(self, renderer, gc):
248248
"""
249249

250250

251+
trans = self.get_transform() #axes.transData
252+
253+
# convert the coordinates to the intermediate coordinate (ic).
254+
# The transformation from the ic to the canvas is a pure
255+
# affine transfor.
256+
257+
# A straight-forward way is to use the non-affine part of the
258+
# original transform for conversion to the ic.
259+
260+
# firs, convert the image extent to the ic
261+
x_llc, x_trc, y_llc, y_trc = self.get_extent()
262+
263+
xy = trans.transform_non_affine(np.array([(x_llc, y_llc),
264+
(x_trc, y_trc)]))
265+
266+
_xx1, _yy1 = xy[0]
267+
_xx2, _yy2 = xy[1]
268+
269+
extent_in_ic = _xx1, _xx2, _yy1, _yy2
270+
271+
# define trans_ic_to_canvas : unless _image_skew_coordinate is
272+
# set, it is simply a affine part of the original transform.
273+
if self._image_skew_coordinate:
274+
# skew the image when required.
275+
x_lrc, y_lrc = self._image_skew_coordinate
276+
xy2 = trans.transform_non_affine(np.array([(x_lrc, y_lrc)]))
277+
_xx3, _yy3 = xy2[0]
278+
279+
tr_rotate_skew = self._get_rotate_and_skew_transform(_xx1, _yy1,
280+
_xx2, _yy2,
281+
_xx3, _yy3)
282+
trans_ic_to_canvas = tr_rotate_skew+trans.get_affine()
283+
else:
284+
trans_ic_to_canvas = trans.get_affine()
285+
286+
# Now, viewLim in the ic. It can be roated and can be
287+
# skewed. Make it big enough.
288+
x1, y1, x2, y2 = self.axes.bbox.extents
289+
trans_canvas_to_ic = trans_ic_to_canvas.inverted()
290+
xy_ = trans_canvas_to_ic.transform(np.array([(x1, y1),
291+
(x2, y1),
292+
(x2, y2),
293+
(x1, y2)]))
294+
x1_, x2_ = min(xy_[:,0]), max(xy_[:,0])
295+
y1_, y2_ = min(xy_[:,1]), max(xy_[:,1])
296+
viewLim_in_ic = Bbox.from_extents(x1_, y1_, x2_, y2_)
297+
298+
299+
# get the image, sliced if necessary. This is done in the ic.
251300
im, xmin, ymin, dxintv, dyintv, sx, sy = \
252-
self._get_unsampled_image(self._A, self.get_extent(),
253-
self.axes.viewLim, noslice=True)
301+
self._get_unsampled_image(self._A, extent_in_ic, viewLim_in_ic)
254302

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

@@ -262,33 +310,14 @@ def _draw_unsampled_image(self, renderer, gc):
262310
im.reset_matrix()
263311
numrows, numcols = im.get_size()
264312

265-
im.resize(numcols, numrows) # just to create im.bufOut that is required by backends. There may be better solution -JJL
313+
im.resize(numcols, numrows) # just to create im.bufOut that
314+
# is required by backends. There
315+
# may be better solution -JJL
266316

267317
im._url = self.get_url()
268318

269-
trans = self.get_transform() #axes.transData
270-
xy = trans.transform_non_affine(np.array([(xmin, ymin),
271-
(xmin+dxintv, ymin+dyintv)]))
272-
xx1, yy1 = xy[0]
273-
xx2, yy2 = xy[1]
274-
275-
if self._image_skew_coordinate:
276-
# skew the image when required.
277-
x_llc, x_trc, y_llc, y_trc = self.get_extent()
278-
x_lrc, y_lrc = self._image_skew_coordinate
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)
287-
tr = tr_rotate_skew+trans.get_affine()
288-
else:
289-
tr = trans.get_affine()
290-
291-
renderer.draw_image(gc, xx1, yy1, im, xx2-xx1, yy2-yy1, tr)
319+
renderer.draw_image(gc, xmin, ymin, im, dxintv, dyintv,
320+
trans_ic_to_canvas)
292321

293322

294323
def _check_unsampled_image(self, renderer):

0 commit comments

Comments
 (0)