Skip to content
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

Easy fix for clipping misrendering of matplotlib's SVG in other viewers #2277

Closed
marklundeberg opened this issue Aug 4, 2013 · 4 comments
Closed

Comments

@marklundeberg
Copy link

I've been noticing lately that matplotlib's SVG output appears with incorrect clipping in a lot of SVG viewers (rsvg-view, Ristretto, gpicview), even though it does appear correctly in good SVG viewers like inkscape, firefox, chrome. The data curves peek outside of the axes. Luckily there is an fix in matplotlib that should be easy enough to implement:

Currently, the SVG structure looks more or less like this, with the clipPath defined at the end:

<svg ..>
 <g id="figure_1">
  <g id="axes_1">
  .... curves drawn in here, referencing clip-path p7ff5b81e1d
  </g>
 </g>
 <defs>
  <clipPath id="p7ff5b81e1d">
   <rect height="345.6" width="446.4" x="72.0" y="43.2"/>
  </clipPath>
 </defs>
</svg>

I found that if I simply moved the section containing that clipPath earlier in the file, then the SVG views just fine in any viewer that I can find. I guess that these basic SVG viewers don't like forward references.

For examples, this works:

<svg ..>
 <g id="figure_1">
 <defs>
  <clipPath id="p7ff5b81e1d">
   <rect height="345.6" width="446.4" x="72.0" y="43.2"/>
  </clipPath>
 </defs>
  <g id="axes_1">
  .... curves drawn in here, referencing clip-path p7ff5b81e1d
  </g>
 </g>
</svg>

I would attach an example SVG but github doesn't seem to support that...

@mdboom
Copy link
Member

mdboom commented Aug 5, 2013

Unfortunately, it's not so simple. The SVG backend currently works in streaming mode, meaning it writes the file in a single pass. This is important for performance reasons when running inside a webserver, for example. We need to find a solution that doesn't involve multiple passes of the output.

Since there is nothing against forward references in the SVG spec, I think the failing viewers are violating the spec and should be fixed (if possible) accordingly. rsvg (on which all of the tools you listed are based, IIRC) has never been the best tool for SVG rendering, and it has a number of other bugs as well.

@marklundeberg
Copy link
Author

Hmm, I thought so, but is it possible to write the axis clipping area before writing the lines? Since it's known beforehand, I mean.

Anyway, if it's not an easy fix, it certainly shouldn't be your burden to have to make workarounds for rsvg. (I didn't know all the viewers were all based on that, makes sense.) Spend your time on keeping matplotlib the best 2D plotting environment ever, and I'll go bug the rsvg guys instead. :P

@mdboom
Copy link
Member

mdboom commented Aug 5, 2013

It's been a while since I've looked into this problem. I'll keep this bug open to remind myself to look again when I have a chance.

@tacaswell
Copy link
Member

Duplicate of #4341, closing in favor of that one.

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

No branches or pull requests

3 participants