有沒有辦法在將matplotlib圖保存到svg時設置miterlimit?我想以編程方式解決此錯誤:https://bugs.launchpad.net/inkscape/+bug/1533058 而我不得不在「.svg」文本文件中手動更改此設置。如何在使用matplotlib保存爲svg時設置miterlimit
2
A
回答
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()
相關問題
- 1. 如何使用snapsvg,js保存SVG?
- 2. 如何在使用Watir時保存Chrome的設置?
- 3. 如何在使用jsp/servlet保存圖像時設置路徑?
- 4. 何時保存設置?
- 5. 如何設置matplotlib
- 6. 如何使用MATPLOTLIB設置標籤?
- 7. 如何使用matplotlib設置偏移
- 8. 如何保存Matplotlib圖
- 9. 將matplotlib圖保存爲tiff時出錯
- 10. 當我爲matplotlib設置一個圖時,如何在一個圖
- 11. 如何使用Makefile和Inkscape保存爲「Plain SVG」
- 12. 如何保存Report.Document.Printer設置?
- 13. 如何保存GVIM設置?
- 14. 如何保存fbset設置?
- 15. 如何在matplotlib中設置偏置軸?
- 16. 如何設置應用程序設置而不保存在c#
- 17. 如何設置時間間隔x軸,說使用matplotlib
- 18. 保存PDF時matplotlib線寬
- 19. c#讀取設置時保存設置
- 20. 如何將svg代碼保存爲.svg圖像?
- 21. 何時保存Apple設置應用程序的設置?
- 22. 用PhantomJS保存SVG
- 23. 如何保存應用程序設置?
- 24. 如何使用matplotlib在python中保存繪圖?
- 25. 在Python中,如何使用matplotlib保存方形圖像?
- 26. 如何在gdb中保存設置?
- 27. 如何在vb.net中保存設置
- 28. 保存div的svg爲pdf
- 29. 保存System.Windows.Forms.Keys作爲設置
- 30. 保存設置爲NSUserDefaults