Skip to content

Commit

Permalink
Add more transforms to affine image transform demo
Browse files Browse the repository at this point in the history
  • Loading branch information
f0k committed Jul 19, 2016
1 parent a43e3a0 commit be54fae
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions examples/api/demo_affine_image.py
@@ -1,11 +1,10 @@
"""
For the backends that supports draw_image with optional affine
For the backends that support draw_image with optional affine
transform (e.g., agg, ps backend), the image of the output should
have its boundary matches the red rectangles.
have its boundary match the dashed yellow rectangle.
"""

import numpy as np
import matplotlib.cm as cm
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
import matplotlib.transforms as mtransforms
Expand All @@ -21,25 +20,37 @@ def get_image():
return Z


if 1:
def do_plot(ax, Z, transform):
im = ax.imshow(Z, interpolation='none',
origin='lower',
extent=[-2, 4, -3, 2], clip_on=True)

trans_data = transform + ax.transData
im.set_transform(trans_data)

# display intended extent of the image
x1, x2, y1, y2 = im.get_extent()
ax.plot([x1, x2, x2, x1, x1], [y1, y1, y2, y2, y1], "y--",
transform=trans_data)
ax.set_xlim(-5, 5)
ax.set_ylim(-4, 4)

# image rotation

fig, ax1 = plt.subplots(1, 1)
if 1:
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
Z = get_image()
im1 = ax1.imshow(Z, interpolation='none',
origin='lower',
extent=[-2, 4, -3, 2], clip_on=True)

trans_data2 = mtransforms.Affine2D().rotate_deg(30) + ax1.transData
im1.set_transform(trans_data2)
# image rotation
do_plot(ax1, Z, mtransforms.Affine2D().rotate_deg(30))

# image skew
do_plot(ax2, Z, mtransforms.Affine2D().skew_deg(30, 15))

# display intended extent of the image
x1, x2, y1, y2 = im1.get_extent()
x3, y3 = x2, y1
# scale and reflection
do_plot(ax3, Z, mtransforms.Affine2D().scale(-1, .5))

ax1.plot([x1, x2, x2, x1, x1], [y1, y1, y2, y2, y1], "--",
transform=trans_data2)
# everything and a translation
do_plot(ax4, Z, mtransforms.Affine2D().
rotate_deg(30).skew_deg(30, 15).scale(-1, .5).translate(.5, -1))

ax1.set_xlim(-3, 5)
ax1.set_ylim(-4, 4)
plt.show()

0 comments on commit be54fae

Please sign in to comment.