Asked By – Emmet B
To remove frame in figure, I write
works perfect with
pyplot.figure, but with
matplotlib.Figure it only removes the gray background, the frame stays . Also, I only want the lines to show, and all the rest of figure be transparent.
with pyplot I can do what I want, I want to do it with matplotlib for some long reason I ‘d rather not mention to extend my question.
Now we will see solution for issue: How to remove frame from matplotlib (pyplot.figure vs matplotlib.figure ) (frameon=False Problematic in matplotlib)
First off, if you’re using
savefig, be aware that it will override the figure’s background color when saving unless you specify otherwise (e.g.
However, to remove the axes’ and figure’s background on-screen, you’ll need to set both
fig.patch to be invisible.
import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.plot(range(10)) for item in [fig, ax]: item.patch.set_visible(False) with open('test.png', 'w') as outfile: fig.canvas.print_png(outfile)
(Of course, you can’t tell the difference on SO’s white background, but everything is transparent…)
If you don’t want to show anything other than the line, turn the axis off as well using
import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.plot(range(10)) fig.patch.set_visible(False) ax.axis('off') with open('test.png', 'w') as outfile: fig.canvas.print_png(outfile)
In that case, though, you may want to make the axes take up the full figure. If you manually specify the location of the axes, you can tell it to take up the full figure (alternately, you can use
subplots_adjust, but this is simpler for the case of a single axes).
import matplotlib.pyplot as plt fig = plt.figure(frameon=False) ax = fig.add_axes([0, 0, 1, 1]) ax.axis('off') ax.plot(range(10)) with open('test.png', 'w') as outfile: fig.canvas.print_png(outfile)