4
votes

I'm having an issue when trying to save a bar plot as an svg. Specifically, when I save the bar plot as a PDF with savefig, I get the correct result. However, when I save it as an SVG, the bars do not terminate at the x-axis as they should but descend past the bottom of the figure. This problem only occurs when I use the log scale for the bar plot. Otherwise, everything is hunky dory.

Here is the code saving the plot as both an SVG and a PDF:

import matplotlib.pyplot as plt
import numpy as np

N = 10
ind = np.arange(N)
series1 = xrange(N)
series2 = [ N - s1_i for s1_i in series1 ]
fig, ax = plt.subplots()
width = 0.2
rects = [ ax.bar(ind, series1, width, color='r', log=True), 
  ax.bar(ind + width, series2, width, color='b', log=True) ]

plt.savefig("test.pdf")
plt.savefig("test.svg")

Here are the two sample images: The PDF version

You can see there are no glaring issues with the PDF version.

The SVG version

The SVG version has bars that are not properly clipped, which is wrong.

Update: In response to tcaswell

I'm using Ubuntu 14.04 (kernel version is 3.16.0) with Python 2.7.6, Matplotlib version 1.3.1, numpy version 1.8.2.

I've tried viewing the SVG both with display and rsvg-view-3, and both show the same result; if I convert it to a PDF using ImageMagick's convert command line tool and open it with evince or another viewer such as acroread, the image remains flawed (not particularly surprising).

1
Does it depend on the viewer you use? The issue is probably bad clipping of the bars and there are issues with viewers not clipping properly depending on the location in the svg file that the clip path is specified (and to be clear, the svg spec says it can be declared anywhere). What version of mpl? - tacaswell
Link to the relevant discussion on the matplotlib issue tracker: github.com/matplotlib/matplotlib/issues/4341 - ali_m
This renders perfectly fine in my browsers (chrome and firefox) on Windows - 7hibault

1 Answers

3
votes

This is a known bug in librsvg (and limitation in libQtSvg which only handles a very restricted sub-set of SVG (1.2 tiny) which does not include clipping at all).

The svg will render correctly in any modern browser.

There is a much longer discussion at https://github.com/matplotlib/matplotlib/issues/4341, but the long and short of it is that the renderer is buggy.