Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用matplotlib另存为svg时如何设置MiterLit_Python_Svg_Matplotlib - Fatal编程技术网

Python 使用matplotlib另存为svg时如何设置MiterLit

Python 使用matplotlib另存为svg时如何设置MiterLit,python,svg,matplotlib,Python,Svg,Matplotlib,在将matplotlib绘图保存到svg时,是否仍要设置斜接限制?我想通过编程绕过这个bug: 然而,我必须在“.svg”文本文件中手动更改此参数。此参数定义为'stroke-miterlit':'100000',并在backend_svg.py中硬设置。matplotlibrc中没有此类参数,因此不可能使用样式表进行自定义 我使用以下代码修复此问题: def fixmiterlimit(svgdata, miterlimit = 10): # miterlimit variable s

在将matplotlib绘图保存到svg时,是否仍要设置斜接限制?我想通过编程绕过这个bug:
然而,我必须在“.svg”文本文件中手动更改此参数。

此参数定义为
'stroke-miterlit':'100000'
,并在backend_svg.py中硬设置。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
然后这样称呼它(使用下面的技巧):


该代码在将SVG输出写入文件之前,基本上更改了SVG输出中的stroke miterlimit参数。为我工作。

感谢drYG给出了很好的答案,感谢aeolus提出了这个问题。我有一个类似的问题,这是由于inkscape错误和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()
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()