Skip to content

Cannot save image data (imshow) as SVG with interpolation='none' #925

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

Merged
merged 1 commit into from
Jun 13, 2012

Conversation

mdboom
Copy link
Member

@mdboom mdboom commented Jun 12, 2012

It seems to me that saving image data (i.e. using imshow) as SVG is broken when using interpolation='none' (matplotlib 1.1 under Linux).

This code

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111)
ax.imshow(np.arange(100).reshape((1, 100)), aspect='auto', origin='lower', interpolation='none')

for extension in ['svg', 'png']:
    fig.savefig('imshow.%s' % extension)

creates a file imshow.png (same for pdf, etc.) where the values are increasing from left to right as they should. The file imshow.svg when viewed with eog shows values decreasing from left to right and also bleeding over the image edges on top and bottom. When viewing the file with firefox or inkscape, there is no image data to be seen at all, only an empty frame...

@ghost ghost assigned mdboom Jun 6, 2012
@mdboom
Copy link
Member

mdboom commented Jun 6, 2012

I can't seem to reproduce this with 1.1, 1.1.x or master. The SVG looks fine in Inkscape, Firefox and Chrome here. Can you put your svg file up somewhere for comparison?

@mstimberg
Copy link
Author

Thanks for having a look into this. I realized that the problem has to do with some settings in my matplotlibrc, in particular I set interpolation='none'. If using interpolation='nearest' instead (which I should probably use), everything looks fine. With interpolation='none' there seem to be actually two separate issues:

  • combining interpolation='none' with aspect='auto' leads to the "bleeding" over the edges (but still correct order of values)
  • combining interpolation='none' with origin='lower' leads to the left-right flipping of the image

I uploaded an SVG produced with all settings (aspect='auto', origin='lower', interpolation='none', I updated the code above), leading to the two issues at the same time: https://gist.github.com/2883752

@mdboom
Copy link
Member

mdboom commented Jun 12, 2012

Can you verify that the attached pull request resolves your issue?

@mdboom
Copy link
Member

mdboom commented Jun 12, 2012

I forgot to note:

This attached patch addresses the upside down image when origin='lower'.

The other issue, that the image bleeds outside of the axes, may have something to do with your SVG renderer. rsvg (on which evince is based) generally doesn't handle SVG clipping correctly -- there's not much we can do to work around that bug. Chrome, Firefox and Inkscape, however, render the image correctly for me.

@mstimberg
Copy link
Author

Many thanks, the commit fixes the wrong order for me. You are also right about the bleeding, I only observe it with eog (which I only used for quick previews, anyway), not with inkscape or firefox.

mdboom added a commit that referenced this pull request Jun 13, 2012
Cannot save image data (imshow) as SVG with interpolation='none'
@mdboom mdboom merged commit 88e5aae into matplotlib:master Jun 13, 2012
@mdboom mdboom deleted the svg-image-origin branch March 3, 2015 18:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants