Skip to content

Data points beyond axes range plotted when saved to SVG #5925

Closed

Description

@sndrtj

Data points that lie beyond the axes range are still visible when using savefig to an SVG file. I thought this was solved by #2423, but I'm still getting this as of version 1.5.1.

Consider the following snippet:

import matplotlib
import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(8.0, 5.0))
ax = fig.add_subplot(111)
ax.plot(range(10), [x**2 for x in range(10)])
ax.set_ylim(0, 50)
plt.savefig("test.svg")

This produces the following output:

screenshot from 2016-01-27 16 06 59

The correct output should have been:

screenshot from 2016-01-27 16 08 32

Activity

  1. mdboom commented on Jan 27, 2016

    @mdboom
    Member

    What are you using to view the SVG? Is it based on librsvg (e.g. most system viewers on Linux)? If so, this was fixed in librsvg 2.40.12. See #4341 for more info.

    If you're using a browser or Inkscape, then this is not a known bug and feel free to reopen.

  2. sndrtj commented on Jan 28, 2016

    @sndrtj
    Author

    This was indeed rendered by the Gnome image viewer. Firefox renders it correctly. Thanks!

  3. cfbrasz commented on Apr 7, 2017

    @cfbrasz

    I was struggling with this same problem for a while (and wasn't sure how to check what version of librsvg I had) and think I've found a strange related bug in inkscape: when I open an svg (like test.svg above) normally, it works fine. When I click and drag the file from nautilus (I'm running Ubuntu 16.04) to an already open inkscape window, then I get the same clipping error reported here.

    It's a very specific case that most people probably won't encounter and it's easy to avoid it by opening the file in any other way, but I figured I'd report it anyways. Sorry if this is the wrong place for it. (It's really an Inkscape bug, right?)

  4. tacaswell commented on Apr 7, 2017

    @tacaswell
    Member

    That should probably be reported to the inkscape folks.

  5. adiMasonD commented on May 12, 2025

    @adiMasonD

    This still appears when viewing the svg in Adobe Illustrator or Edge.

  6. joey-kilgore commented on Jun 5, 2025

    @joey-kilgore

    I have been dealing with this kind of problem for a while. I will take an SVG and bring it into powerpoint and then break it apart so I can edit the individual pieces (I know... I hate powerpoint too, but it's what I need to use for work). However, if I just use xlim or ylim, the data points still exist in the SVG outside the range and are then shown. This means that data is still being saved in the .svg file that should not be there when using xlim and ylim.

    I don't think this is a problem with the individual editors, because the data for these extra lines (or dots if using scatter) exist because there is additional data written in the SVG file that should not be there. Yes, certain editors do correctly render these images (for instance if I don't break the SVG apart in powerpoint it looks completely fine) but the fact that additional data is being written to file that shouldn't be means there is an underlying issue here.

    I'm not sure if there is something else I am doing wrong, but I think it should be the case that if I call .xlim or .ylim, then the data outside those bounds should not be included in the saved file.

  7. jklymak commented on Jun 5, 2025

    @jklymak
    Member

    This means that data is still being saved in the .svg file that should not be there when using xlim and ylim.

    We don't throw data out - we just limit the view window using the SVG viewBox. We can't throw the data out in case you pan or zoom the image, and while I suppose you could argue that we should remove it when we save, we just don't since SVG (and other vector graphics languages) explicitly support view windows for this purpose, and that is much easier than Matplotlib trying to crop every object that may overspill the axes boundaries.

    You can either crop the data yourself before you plot it, or make a new viewBox in Powerpoint (something like Shape Format → Merge Shapes → Intersect.).

  8. joey-kilgore commented on Jun 5, 2025

    @joey-kilgore

    @jklymak Thank you so much for the explanation! Will have to try this idea for some kind of shape intersect in PowerPoint.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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