简体   繁体   English

从 plot 中删除默认补丁

[英]Remove default patches from plot

I want to export a plot that contains no "unnecessary" patches.我想导出一个不包含“不必要”补丁的 plot。 The following code exports a figure that has invisible patches with zero alpha:以下代码导出具有零 alpha 不可见补丁的图形:

plt.title("Test")
plt.plot()
plt.gca().patch.set_alpha(0.)
plt.gcf().patch.set_alpha(0.)
plt.savefig('test.svg')

Although these patches are not visible, their geometry hinders Inkscape from properly resizing the page to its content.尽管这些补丁不可见,但它们的几何形状阻碍了 Inkscape 正确调整页面大小以适应其内容。 Would these "unnecessary" patches be removed the grey area could be cropped using eg Inkscape:是否会删除这些“不必要的”补丁,可以使用例如 Inkscape 裁剪灰色区域:

在此处输入图像描述

How do I either plot without the patches or remove the figure's and axis' patches programmatically?如何在没有补丁的情况下使用 plot 或以编程方式删除图形和轴的补丁?

SVG is a text file. SVG 是一个文本文件。 Use a text editor and delete these parts:使用文本编辑器并删除这些部分:

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

The details can be different, but you can figure out with fill:none in the style.细节可能有所不同,但您可以通过样式中的fill:none弄清楚。

EDIT 1 SVG file can be rendered and edit with Inkscape.编辑 1 SVG 文件可以用 Inkscape 渲染和编辑。 With its XML Editor, it is easy to find all the unneeded elements and delete them in XML Editor.借助其 XML 编辑器,可以轻松找到所有不需要的元素并在 XML 编辑器中删除它们。

This seems to do the trick for this plain MWE.这似乎对这个普通的 MWE 有用。 I hope it fits your needs.我希望它符合您的需求。

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

The strategy is to get the ax bbox and then use it as an argument to savefig (it requires transformation to figure inches).策略是获取 ax bbox,然后将其用作 savefig 的参数(它需要转换为数字英寸)。

More in here:更多在这里:

https://matplotlib.org/3.1.1/api/transformations.html#matplotlib.transforms.TransformedBbox https://matplotlib.org/3.1.1/api/transformations.html#matplotlib.transforms.TransformedBbox

https://matplotlib.org/tutorials/advanced/transforms_tutorial.html https://matplotlib.org/tutorials/advanced/transforms_tutorial.html

EDIT编辑

A cleaner solution:更清洁的解决方案:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

plt.title("Test")
ax.plot()

fig.patch.set_visible(False)

plt.savefig('test.svg',)

The best solution now depends on your specific case.现在最好的解决方案取决于您的具体情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM