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

Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add more transforms to affine image transform demo
  • Loading branch information
f0k committed Aug 16, 2016
commit d931283c9adf082dc9c6ae74a9e9609f6dd306f9
49 changes: 30 additions & 19 deletions examples/api/demo_affine_image.py
Original file line number Diff line number Diff line change
@@ -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)

# image rotation
trans_data = transform + ax.transData
im.set_transform(trans_data)

fig, ax1 = plt.subplots(1, 1)
Z = get_image()
im1 = ax1.imshow(Z, interpolation='none',
origin='lower',
extent=[-2, 4, -3, 2], clip_on=True)
# 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)

trans_data2 = mtransforms.Affine2D().rotate_deg(30) + ax1.transData
im1.set_transform(trans_data2)

# display intended extent of the image
x1, x2, y1, y2 = im1.get_extent()
x3, y3 = x2, y1
# prepare image and figure
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
Z = get_image()

# image rotation
do_plot(ax1, Z, mtransforms.Affine2D().rotate_deg(30))

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

# 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()