[英]Remove default patches from plot
SVG 是一个文本文件。 使用文本编辑器并删除这些部分:
<g id="patch_3">
<path d="M 54 252
L 54 34.56
" style="fill:none;stroke:#000000;stroke-linecap:square;stroke-linejoin:miter;stroke-width:0.8;"/>
</g>
<g id="patch_4">
<path d="M 388.8 252
L 388.8 34.56
" style="fill:none;stroke:#000000;stroke-linecap:square;stroke-linejoin:miter;stroke-width:0.8;"/>
</g>
<g id="patch_5">
<path d="M 54 252
L 388.8 252
" style="fill:none;stroke:#000000;stroke-linecap:square;stroke-linejoin:miter;stroke-width:0.8;"/>
</g>
<g id="patch_6">
<path d="M 54 34.56
L 388.8 34.56
" style="fill:none;stroke:#000000;stroke-linecap:square;stroke-linejoin:miter;stroke-width:0.8;"/>
</g>
细节可能有所不同,但您可以通过样式中的fill:none
弄清楚。
编辑 1 SVG 文件可以用 Inkscape 渲染和编辑。 借助其 XML 编辑器,可以轻松找到所有不需要的元素并在 XML 编辑器中删除它们。
这似乎对这个普通的 MWE 有用。 我希望它符合您的需求。
import matplotlib.pyplot as plt
from matplotlib.transforms import Bbox
from matplotlib.transforms import TransformedBbox
fig, ax = plt.subplots()
plt.title("Test")
ax.plot()
plt.savefig('test.svg', bbox_inches = TransformedBbox(Bbox(ax.bbox.get_points()),transform=fig.dpi_scale_trans.inverted()) )
策略是获取 ax bbox,然后将其用作 savefig 的参数(它需要转换为数字英寸)。
更多在这里:
https://matplotlib.org/3.1.1/api/transformations.html#matplotlib.transforms.TransformedBbox
https://matplotlib.org/tutorials/advanced/transforms_tutorial.html
编辑
更清洁的解决方案:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
plt.title("Test")
ax.plot()
fig.patch.set_visible(False)
plt.savefig('test.svg',)
现在最好的解决方案取决于您的具体情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.