2016-09-08 15 views

回答

1

該參數定義爲'stroke-miterlimit': '100000',在backend_svg.py中設置爲hard。 matplotlibrc中沒有這樣的參數,所以使用樣式表進行自定義不太可能。

我用下面的代碼來解決這個問題:

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 

然後調用它像這樣(與此post的伎倆):

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() 

的代碼基本上改變了卒中在將其寫入文件之前,在SVG輸出中使用miterlimit參數。爲我工作。

1

感謝drYG給出了很好的答案,並提出了問題的答案。我有一個類似的問題,由於這個Inkscape的bug,缺少一個簡單的方法來改變matplotlib中的設置。但是,drYG的答案似乎不適用於python3。我更新了它,並改變了似乎有些拼寫錯誤(不考慮python版本)。希望它能夠在我嘗試彌補我失去的東西的時候拯救別人!

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' 
    return svgout 

import io, re 
imgdata = io.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.gca() 
plt.savefig(imgdata, format='svg', dpi=90, bbox_inches='tight') 
imgdata.seek(0) # rewind the data 
svg_dta = imgdata.getvalue() # this is svg data 
svgoutdata = fixmiterlimit(re.split(r'\n', svg_dta)) # pass as an array of lines 
svgfh = open('test.svg', 'w') 
svgfh.write(svgoutdata) 
svgfh.close()