You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have long had a problem with SVG matplotlib output in the notebook. Intermittently, the plot will extend outside of the axes, as exemplified in this screenshot:
Basically, everything below the horizontal axis should be hidden. Most of the time, it is properly hidden -- e.g., when the plot is first made, or when I collapse and expand the output area. But then sometimes, it'll go buggy like this. I can't reproduce this consistently, but it seems to happen most often when I make a plot in another cell.
That stuff that's usually hidden evidently does correspond to actual data; when I plot again with xlim(xmin=1e-18), the data does show up just like that, except that it is correctly based at 1e-18. So it's evidently really in the SVG.
I'm wondering if this is related to #1881 and #2647, but none of the descriptions of problems there were specific enough. Is this expected badness in the matplotlib output?
I've had this problem for a long time (years?), but I can verify that it exists currently in IPython 3.0.0. I'm seeing it in up-to-date Chrome and Safari on up-to-date OS X, using an up-to-date anaconda installation (python 2.7). Here are the IPython details:
Ah, yes I can! And it's even reproducible! I've never been able to make this reproducible before.
Open this notebook. You might have to evaluate the cells to make sure the output is actually SVG; if so I don't know of any way to do this without the following line in ipython_config.py:
c.InlineBackend.figure_format='svg'
Anyway, once you've got all three figures as SVG, delete the middle one. This does it every time for me. The badness also happens for me in existing figures when I'm evaluating a new one.
After diffing the whole HTML of the notebook before and after deleting the first plot, I wasn't able to find a significant difference.
Yeah, I'd doubt that it'd be HTML; it comes and goes (and comes and goes again) depending on various actions in the notebook.
I think it's really just that the data points are failing to be clipped by the axes rectangle. But why this should fail intermittently is the question.
Another data point: I can use just plain plots, with no log scaling, and get extra data to appear on all four sides of the axes:
So I guess it's just a generic feature of the SVG, whenever the range of the data being plotted is larger than the range of the xlim and ylim values.
Could browser svg rendering be buggy? I don't think there are many svgs out
there on the web, because IE didn't used to support them (possibly still
doesn't), so it may not be something that has received as much attention.
Safari and Chrome both use the Webkit rendering engine (technically, Chrome uses Blink, which is a fork of Webkit), so it's possible that they are inheriting the same underlyign bug.
It is also interesting that the example using plot can reproduce this issue but removing log=True in the other examples avoids the problem.
I think that's just a coincidence of the limits. With log=False, the bars of the histogram range between y=0 and y=1 (or higher), but the limits also go from y=0 to y=1 (or higher), so all of the plotted stuff is within the limits, and there's nothing to be displayed incorrectly. With log=True, the bars extend infinitely(ish) far down, but the ylimit has some finite minimum, so the bars can be accidentally displayed.
That's why I think plot is the more useful case; as long as there's data outside the limits, it looks like there's potential for that data to be accidentally displayed.
No, it doesn't work for me -- unless I'm misunderstanding you. I tried your experiment of copying the SVG twice into an HTML file, then delete one using developer tools. I got the out-of-bounds lines. Then I went into the HTML file and deleted the <defs> sections from both SVGs, full refreshed the page, deleted one, and zoomed, and I saw the problem again. You can see in my screenshot that the defs section isn't there:
That certainly explains that. And for the naive MWE above, I can verify that the clipPath id is the same in each case. But then, I can also change the parameters of the graph and get the same clipPath. So apparently, either whatever is being hashed to produce the id either isn't complete enough, or the hash isn't updated for changes in the plot. Any idea where that id comes from?
Your SVG is defining a clipping mask at the end of the file:
The browser seems to be flexible about this, but you usually want to
create your definitions at the top of the SVG code. Unfortunately,
matplotlib is placing clipping masks at the bottom and this is causing your
problem. For example, if you move your clipping mask as follows:
@abreslow The issue you are seeing with clipping is a bug is librsvg rendering incorrectly, not in mpl emitting buggy svg. There is no constraint that clip paths must appear in the document before they are used. See much longer response at matplotlib/matplotlib#4341
Activity
moble commentedon Mar 24, 2015
Ah, yes I can! And it's even reproducible! I've never been able to make this reproducible before.
Open this notebook. You might have to evaluate the cells to make sure the output is actually SVG; if so I don't know of any way to do this without the following line in
ipython_config.py:Anyway, once you've got all three figures as SVG, delete the middle one. This does it every time for me. The badness also happens for me in existing figures when I'm evaluating a new one.
moble commentedon Mar 25, 2015
Yeah, I'd doubt that it'd be HTML; it comes and goes (and comes and goes again) depending on various actions in the notebook.
I think it's really just that the data points are failing to be clipped by the axes rectangle. But why this should fail intermittently is the question.
Another data point: I can use just plain
plots, with no log scaling, and get extra data to appear on all four sides of the axes:So I guess it's just a generic feature of the SVG, whenever the range of the data being plotted is larger than the range of the
xlimandylimvalues.moble commentedon Mar 25, 2015
I should also point out that I used a completely new profile to do this, with absolutely no changes to
ipython_config.py,custom.js, etc.takluyver commentedon Mar 25, 2015
moble commentedon Mar 25, 2015
Safari has the same problem as Chrome, so I'd be a little surprised if it's a rendering bug -- though not hugely surprised.
BTW I've boiled it down to a particularly simple test case:
takluyver commentedon Mar 25, 2015
Safari and Chrome both use the Webkit rendering engine (technically, Chrome uses Blink, which is a fork of Webkit), so it's possible that they are inheriting the same underlyign bug.
moble commentedon Mar 25, 2015
I think that's just a coincidence of the limits. With
log=False, the bars of the histogram range between y=0 and y=1 (or higher), but the limits also go from y=0 to y=1 (or higher), so all of the plotted stuff is within the limits, and there's nothing to be displayed incorrectly. Withlog=True, the bars extend infinitely(ish) far down, but the ylimit has some finite minimum, so the bars can be accidentally displayed.That's why I think
plotis the more useful case; as long as there's data outside the limits, it looks like there's potential for that data to be accidentally displayed.moble commentedon Apr 4, 2015
No, it doesn't work for me -- unless I'm misunderstanding you. I tried your experiment of copying the SVG twice into an HTML file, then delete one using developer tools. I got the out-of-bounds lines. Then I went into the HTML file and deleted the
<defs>sections from both SVGs, full refreshed the page, deleted one, and zoomed, and I saw the problem again. You can see in my screenshot that thedefssection isn't there:moble commentedon Apr 4, 2015
That certainly explains that. And for the naive MWE above, I can verify that the clipPath id is the same in each case. But then, I can also change the parameters of the graph and get the same clipPath. So apparently, either whatever is being hashed to produce the id either isn't complete enough, or the hash isn't updated for changes in the plot. Any idea where that id comes from?
abreslow commentedon Apr 14, 2015
Can you provide a pointer to that issue? I posted this on StackOverflow: http://stackoverflow.com/questions/29616764/matplotlib-savefig-produces-incorrect-svg-image-for-bar-chart-with-log-scaled-y
abreslow commentedon Apr 15, 2015
Thanks @rsmith31415.
On Tue, Apr 14, 2015 at 1:34 PM, rsmith31415 [email protected]
wrote:
Alex Breslow
PhD student in computer science at UC San Diego
Email: [email protected]
Website: cseweb.ucsd.edu/~abreslow
minrk commentedon Apr 15, 2015
Thanks, folks.
tacaswell commentedon Apr 28, 2015
@abreslow The issue you are seeing with clipping is a bug is librsvg rendering incorrectly, not in mpl emitting buggy svg. There is no constraint that clip paths must appear in the document before they are used. See much longer response at matplotlib/matplotlib#4341
BUG : fix svg corner case
tacaswell commentedon Apr 28, 2015
@abreslow Please see matplotlib/matplotlib#4388 and see if that fixes your problem.