Skip to content

Issues with clipping image in display coordinates #3173

Closed as not planned
Closed as not planned
@astrofrog

Description

@astrofrog

For a package I am developing, I need to clip and image (shown with imshow) in display coordinates. Clipping seems to work fine in pixel coordinates:

import numpy as np
from matplotlib.lines import Path
from matplotlib.patches import PathPatch

import matplotlib.pyplot as plt

x = [0.3, 0.6, 0.6, 0.3, 0.3]
y = [0.3, 0.3, 0.6, 0.6, 0.3]

np.random.seed(12345)

image = np.random.random((12,12))

data = np.vstack([x, y]).transpose()
path = Path(np.vstack(data))
poly = PathPatch(path, edgecolor='red', facecolor='none')

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.add_patch(poly)
im = ax.imshow(image, extent=[0., 1., 0., 1.], interpolation='nearest')
im.set_clip_path(poly)
fig.savefig('test1.png')

test1

but not in display coordinates:

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.add_patch(poly)
im = ax.imshow(image, extent=[0., 1., 0., 1.], interpolation='nearest')

display = ax.transData.transform(data)
path_display = Path(display)
poly_display = PathPatch(path_display, transform=None)

im.set_clip_path(poly_display)

fig.savefig('test2.png')

test2

Is this a bug, or am I doing this incorrectly?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions