0

I want to export a plot that contains no "unnecessary" patches. The following code exports a figure that has invisible patches with zero 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. Would these "unnecessary" patches be removed the grey area could be cropped using e.g. Inkscape:

enter image description here

How do I either plot without the patches or remove the figure's and axis' patches programmatically?

1
  • Adding more requirements on the question should never be done, especially when someone has post an answer. This is simply because one can always ask a new question based on new requirements. Commented Jul 5, 2020 at 10:11

2 Answers 2

1

SVG is a text file. 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.

EDIT 1 SVG file can be rendered and edit with Inkscape. With its XML Editor, it is easy to find all the unneeded elements and delete them in XML Editor.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. Since you answered quite literally I rephrased the question. I did not say that editing things by hand is not an option for me. Sorry for not having pointed this out early enough.
@Felix SVG is an XML dialect. It can be read and parsed. Certain nodes/elements can be pinpointed/edited/deleted. In principle, do it manually/programmatically are possible. Please ask with clear and limited scope. Feel free to ask as a new question. No need to be sorry.
1

This seems to do the trick for this plain 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).

More in here:

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

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.