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

Skip to content

Docstring fix & small adjustements in demo_affine_image #6673

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
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
40 changes: 30 additions & 10 deletions examples/api/demo_affine_image.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"""
For the backends that supports 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 matches the red dashed rectangle. The extent of
the image without affine transform is additionally displayed with
a black solid 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 +22,44 @@ def get_image():
return Z


if 1:
def plot_extent(im, rect_lw=1.5, ls="-", color="Black", transform=None):
"""Draws a rectangle denoting the extent of an image `im` altered by a
transform `transform`. Additional segment markers going through then
origin are also plotted.

`rect_lw` is the linewidth parameter used to the rectangle.
"""
x1, x2, y1, y2 = im.get_extent()
ax = im.axes
if transform is None: # then no specific transform will be applied
transform = ax.transData
# Plot the extent rectangle
ax.plot([x1, x2, x2, x1, x1], [y1, y1, y2, y2, y1], ls=ls, lw=rect_lw,
color=color, transform=transform)
# Plot the segments parallel to the rectangle sides & going through (0, 0)
ax.plot([x1, x2], [0, 0], ls=ls, color=color, transform=transform)
ax.plot([0, 0], [y1, y2], ls=ls, color=color, transform=transform)


# image rotation
if 1:

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)

# Image rotation
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

ax1.plot([x1, x2, x2, x1, x1], [y1, y1, y2, y2, y1], "--",
transform=trans_data2)
# Plot the extent of the image:
# 1/ With the affine transform.
plot_extent(im1, ls="--", rect_lw=3, color="Red", transform=trans_data2)
# 2/ Without the affine transform (see `plot_extent` defaults).
plot_extent(im1)

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

plt.show()