From eaaa791b8c7eff88c153e2635b9edd3f5312f2ac Mon Sep 17 00:00:00 2001 From: Adrien F Vincent Date: Fri, 1 Jul 2016 22:24:24 +0200 Subject: [PATCH 1/2] Docstring fix & small adjustements --- examples/api/demo_affine_image.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/examples/api/demo_affine_image.py b/examples/api/demo_affine_image.py index 384df3845b05..6841cfa9d49c 100644 --- a/examples/api/demo_affine_image.py +++ b/examples/api/demo_affine_image.py @@ -1,7 +1,7 @@ """ 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. """ import numpy as np @@ -23,14 +23,13 @@ def get_image(): if 1: - # image rotation - 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) @@ -38,8 +37,10 @@ def get_image(): x1, x2, y1, y2 = im1.get_extent() x3, y3 = x2, y1 - ax1.plot([x1, x2, x2, x1, x1], [y1, y1, y2, y2, y1], "--", + ax1.plot([x1, x2, x2, x1, x1], [y1, y1, y2, y2, y1], "--r", lw=3, transform=trans_data2) ax1.set_xlim(-3, 5) ax1.set_ylim(-4, 4) + + plt.show() From 2f263b56ddc9ae9c8bab4ef20d830d6f0b626b37 Mon Sep 17 00:00:00 2001 From: Adrien F Vincent Date: Sat, 2 Jul 2016 10:39:44 +0200 Subject: [PATCH 2/2] Factorize plotting extent & add extent w/o affine transform --- examples/api/demo_affine_image.py | 37 +++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/examples/api/demo_affine_image.py b/examples/api/demo_affine_image.py index 6841cfa9d49c..ed34a2431eb0 100644 --- a/examples/api/demo_affine_image.py +++ b/examples/api/demo_affine_image.py @@ -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 dashed rectangle. +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 @@ -21,6 +22,25 @@ def get_image(): return Z +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) + + if 1: fig, ax1 = plt.subplots(1, 1) @@ -29,16 +49,15 @@ def get_image(): origin='lower', extent=[-2, 4, -3, 2], clip_on=True) - # image rotation + # 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], "--r", lw=3, - 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)