why does transform=None cause a patch not to be shown?

For the following code, if I remove the transform=None a green patch is shown. If it is in, it is not shown. I would think that transform=None should have no effect. Why is this?

Thanks,
Che

import matplotlib.pyplot as plt

import matplotlib.patches as patches
from matplotlib.path import Path

fig = plt.figure()
ax = fig.add_subplot(111)

start = 0.2
stop = .6

verts = [
(start, .2), # left, bottom
(start, .4), # left, top

(stop, .4), # right, top
(stop, .2), # right, bottom
(0., 0.), # ignored
]

codes = [Path.MOVETO,
Path.LINETO,
Path.LINETO,
Path.LINETO,
Path.CLOSEPOLY,

     ]

path = Path(verts, codes)

patch = patches.PathPatch(path, facecolor=‘g’,
lw=1, edgecolor=‘grey’,transform=None )

ax.add_patch(patch)

plt.show()

For the following code, if I remove the transform=None a green patch is shown. If it is in, it is not shown. I would think that transform=None should have no effect. Why is this?

This is a weird one: When you set the transform, the artist notices (sets the _transformSet flag) and remembers that you set it. If the axes sees that you’ve set the artist’s transform, it won’t override the artist’s transform with the data transform (which is what properly scales your patch to the data coordinates).

Best,

-Tony

···

On Thu, Mar 22, 2012 at 1:43 AM, C M <cmpython@…287…> wrote:

Thanks,
Che

import matplotlib.pyplot as plt

import matplotlib.patches as patches
from matplotlib.path import Path

fig = plt.figure()
ax = fig.add_subplot(111)

start = 0.2
stop = .6

verts = [
(start, .2), # left, bottom
(start, .4), # left, top

(stop, .4), # right, top
(stop, .2), # right, bottom
(0., 0.), # ignored
]

codes = [Path.MOVETO,
Path.LINETO,
Path.LINETO,
Path.LINETO,
Path.CLOSEPOLY,

     ]

path = Path(verts, codes)

patch = patches.PathPatch(path, facecolor=‘g’,
lw=1, edgecolor=‘grey’,transform=None )

ax.add_patch(patch)

plt.show()


This SF email is sponsosred by:

Try Windows Azure free for 90 days Click Here

http://p.sf.net/sfu/sfd2d-msazure


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Hello,

Not sure this is related, but it's also a behaviour I don't understand
when adding a patch.
Tony Yu has been providing many insightful explanations on artists,
but I'm not there yet...
So my apologies if this has been discussed already on this mailing list.

I'm plotting two subplots and I would like to draw a given rectangle
on both of them.
The rectangle displays if I "add_patch" it to the first subplot only.
It does not display (on either subplot) if I "add_patch" it to both subplots.

I don't understand why this is. Is this an intentional behaviour?

Below is my script:

-----------------------------------8<-----------------------------------
import matplotlib.pyplot as plt
from scipy import ndimage
from matplotlib import patches

# Next 8 lines don't pertain to the issue--skip.
initmid = plt.imread('movie2_t001_z005_c001.png')
aftermid = plt.imread('movie2_t002_z005_c001.png')
init2 = ndimage.gaussian_filter(initmid, 1)
after2 = ndimage.gaussian_filter(aftermid, 1)
x_in = 580
y_in = 280
dx_in = 40
dy_in = 40

# Draw rectangles to visualize the area of interest:
rect_in = plt.Rectangle((x_in, y_in), dx_in, dy_in,
                        edgecolor='g', facecolor='none')

# Plot 2 different views:
fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2)
plt.gray()

ax1.imshow(init2)
ax1.add_patch(rect_in)

ax2.imshow(after2)
# ax2.add_patch(rect_in)
plt.show()
-----------------------------------8<-----------------------------------

If I uncomment the second to last line, I don't get any green
rectangle any more.

Thanks,
Marianne

The key thing to know about normal Artists is that they can have just one transform (to take an artist’s coordinates into pixel space), so whilst there is no error when you do it, it is not possible to add the same artist to multiple Axes and have the desired effect.

To answer your question, try creating two Rectangles and adding one to each of your Axes.

HTH