Skip to content

multiple imshow in figure are rendered as single image in svg, causing problems when clipping images #7151

Closed
@luca-penasa

Description

@luca-penasa

I am on matplotlib 1.5.1 (but i suppose this is not related with this specific version)
This is probably a bug/limit of the svg export engine (maybe known?)

when multiple calls to imshow are made in the same figure and then saved in svg ALL the calls are joined into a single raster instead of having multiple (e.g. grouped) images in the svg file.

This make fail a simple test case like this. We call two times imshow to position two images in the figure, then we clip the two images with two different shapes. The problem is that in the svg the two imshow calls are joined in an unique raster, thus the clips are simply not created at all in the svg. Exporting each imshow() raster as a separated image in the svg, each with their clips, would probably solve the problem.
Is it possible or is a known limit?

if saved as png:
test

and as svg:
test_svg

Code for generating these pictures (for a notebook):

%pylab inline

import matplotlib

pylab.rcParams['figure.figsize'] = (8, 8)

a = np.random.randn(100,100)
b = np.random.randn(100,100)

im1 = imshow(a, extent = [0,1,0,1])
im2 = imshow(b, extent = [1.2,2.2,1.2,2.2])
xlim(0,2.2)
ylim(0,2.2)

from matplotlib.path import Path
verts1 = np.array([[0.5,0.5],[1,0.5],[1,1], [0,0.5], [0.5,0.5]])
path1 = Path(verts1)
path2 = Path(verts1 + np.array([1.1, 1.2]))


import matplotlib.patches as patches
patch1 = patches.PathPatch(path1, facecolor=None, alpha=0)
gca().add_patch(patch1)

patch2 = patches.PathPatch(path2, facecolor=None, alpha=0)
gca().add_patch(patch2)

im1.set_clip_path(patch1)
im2.set_clip_path(patch2)

savefig("test.png")
savefig("test.svg")

many thanks for the wonderful work 👍

Luca

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions