How to set miterlimit when saving as svg with matplotlib
<p>Is there anyway to set the miterlimit when saving a matplotlib plot to svg? I'd like to programatically get around this bug:<a href="https://bugs.launchpad.net/inkscape/+bug/1533058" rel="nofollow">https://bugs.launchpad.net/inkscape/+bug/1533058</a>
whereas I am having to manually change this in the ".svg" text file.</p>

<p>This parameter is defined as <code>'stroke-miterlimit': '100000'</code> and is hard-set in backend_svg.py. There is no such parameter in matplotlibrc, so customizing with style sheet is unlikely to be possible.</p>

<p>I used the following code to fix this issue:</p>

<pre><code>def fixmiterlimit(svgdata, miterlimit = 10):
    # miterlimit variable sets the desired miterlimit
    mlfound = False
    svgout = ""
    for line in svgdata:
        if not mlfound:
             # searches the stroke-miterlimit within the current line and changes its value
             mlstring = re.subn(r'stroke-miterlimit:([0-9]+)', "stroke-miterlimit:" + str(miterlimit), line)
        if mlstring[1]: # use number of changes made to the line to check whether anything was found
            mlfound = True
            svgout += mlstring[0] + '\n'
        else:
            svgout += line + '\n'
    else:
        svgout += line + '\n'
return svgout
</code></pre>

<p>And then call it like this (with the trick from this <a href="http://stackoverflow.com/questions/5453375/matplotlib-svg-as-string-and-not-a-file?rq=1">post</a>):</p>

<pre><code>import StringIO
...
imgdata = StringIO.StringIO() # initiate StringIO to write figure data to
# the same you would use to save your figure to svg, but instead of filename use StringIO object
plt.savefig(imgdata, format='svg',  dpi=90, bbox_inches='tight')
imgdata.seek(0)  # rewind the data
svg_dta = imgdata.buf  # this is svg data

svgoutdata = fixmiter(re.split(r'\n', svg_dta)) # pass as an array of lines
svgfh = open('figure1.svg', 'w')
svgfh.write(svgoutdata)
svgfh.close()
</code></pre>

<p>The code basically changes the stroke-miterlimit parameter in SVG output before writing it to file. Worked for me.</p>

python svg matplotlib