Hi, this is a question I have posted earlier, but
> unfortunately I didn't get any answer. if anybody has any
> hint on how to do this, I would be most graceful!! Thanks
> in advance!
I looked at this a bit -- the underlying image extension code handles
image rotations but it is not exposed at the python level. I spent
some time working on an image class that would handle rotations (in
this test code below I just hardcoded the rotation for testing). The
missing part is to get the extent and image placement algorithms to do
the layout properly in the presence of rotation (eg handling extent
and corners properly below). But this should give the enterprising
developer a head start if they want to run with with. Basically, I
just copied the guts out of the axes.image.AxesImage.make_image code
to experiment with adding a rotation
from matplotlib.image import AxesImage
from pylab import subplot, show, nx
class RotatedImage(AxesImage):
def make_image(self):
from matplotlib.colors import normalize, colorConverter
from matplotlib.numerix import arange, asarray, UInt8, Float32, repeat, NewAxis, typecode
import matplotlib._image as _image
if self._A is not None:
if self._imcache is None:
if typecode(self._A) == UInt8:
im = _image.frombyte(self._A, 0)
im.is_grayscale = False
else:
x = self.to_rgba(self._A, self._alpha)
im = _image.fromarray(x, 0)
if len(self._A.shape) == 2:
im.is_grayscale = self.cmap.is_gray()
else:
im.is_grayscale = False
self._imcache = im
else:
im = self._imcache
else:
raise RuntimeError('You must first set the image array or the image attribute')
bg = colorConverter.to_rgba(self.axes.get_frame().get_facecolor(), 0)
if self.origin=='upper':
im.flipud_in()
im.set_bg( *bg)
im.set_interpolation(self._interpd[self._interpolation])
# image input dimensions
numrows, numcols = im.get_size()
im.reset_matrix()
xmin, xmax, ymin, ymax = self.get_extent()
dxintv = xmax-xmin
dyintv = ymax-ymin
# the viewport scale factor
sx = dxintv/self.axes.viewLim.width()
sy = dyintv/self.axes.viewLim.height()
if im.get_interpolation()!=_image.NEAREST:
im.apply_translation(-1, -1)
# the viewport translation
tx = (xmin-self.axes.viewLim.xmin())/dxintv * numcols
#if flipy:
# ty = -(ymax-self.axes.viewLim.ymax())/dyintv * numrows
#else:
# ty = (ymin-self.axes.viewLim.ymin())/dyintv * numrows
ty = (ymin-self.axes.viewLim.ymin())/dyintv * numrows
l, b, widthDisplay, heightDisplay = self.axes.bbox.get_bounds()
im.apply_translation(tx, ty)
im.apply_scaling(sx, sy)
# resize viewport to display
rx = widthDisplay / numcols
ry = heightDisplay / numrows
im.apply_scaling(rx, ry)
im.apply_rotation(45.)
#print tx, ty, sx, sy, rx, ry, widthDisplay, heightDisplay
im.resize(int(widthDisplay+0.5), int(heightDisplay+0.5),
norm=self._filternorm, radius=self._filterrad)
if self.origin=='upper':
im.flipud_in()
return im
ax = subplot(111)
im = RotatedImage(ax, interpolation='nearest')
im.set_data(nx.mlab.rand(10,10))
xmin, xmax, ymin, ymax = im.get_extent()
corners = (xmin, ymin), (xmax, ymax)
ax.update_datalim(corners)
ax.set_xlim((xmin, xmax))
ax.set_ylim((ymin, ymax))
ax.images.append(im)
show()