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.
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. fig.savefig('blah.png', transparent=True)).
However, to remove the axes’ and figure’s background on-screen, you’ll need to set both ax.patch and fig.patch to be invisible.
E.g.
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 ax.axis('off'):
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)
ax.axis('off'), will as Joe Kington pointed out, remove everything except the plotted line.
For those wanting to only remove the frame (border), and keep labels, tickers etc, one can do that by accessing the spines object on the axis. Given an axis object ax, the following should remove borders on all four sides:
import matplotlib.pyplot as plt
import numpy as np
plt.figure()
xvals = list('ABCDE')
yvals = np.array(range(1,6))
position = np.arange(len(xvals))
mybars = plt.bar(position, yvals, align='center', linewidth=0)
plt.xticks(position, xvals)
plt.title('My great data')# plt.show()# get rid of the framefor spine in plt.gca().spines.values():
spine.set_visible(False)# plt.show()# remove all the ticks and directly label each bar with respective value
plt.tick_params(top='off', bottom='off', left='off', right='off', labelleft='off', labelbottom='on')# plt.show()# direct label each bar with Y axis valuesfor bari in mybars:
height = bari.get_height()
plt.gca().text(bari.get_x()+ bari.get_width()/2, bari.get_height()-0.2, str(int(height)),
ha='center', color='white', fontsize=15)
plt.show()
In this case, one can then label the bars directly; the final plot could look like this (code can be found below):
Here is the entire code that is necessary to generate the plots:
import matplotlib.pyplot as plt
import numpy as np
plt.figure()
xvals = list('ABCDE')
yvals = np.array(range(1, 6))
position = np.arange(len(xvals))
mybars = plt.bar(position, yvals, align='center', linewidth=0)
plt.xticks(position, xvals)
plt.title('My great data')
# plt.show()
# get rid of the frame
for spine in plt.gca().spines.values():
spine.set_visible(False)
# plt.show()
# remove all the ticks and directly label each bar with respective value
plt.tick_params(top='off', bottom='off', left='off', right='off', labelleft='off', labelbottom='on')
# plt.show()
# direct label each bar with Y axis values
for bari in mybars:
height = bari.get_height()
plt.gca().text(bari.get_x() + bari.get_width()/2, bari.get_height()-0.2, str(int(height)),
ha='center', color='white', fontsize=15)
plt.show()
data = range(100)import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(data)#ax.set(frameon=False) # Old
ax.set(frame_on=False)# New
plt.show()
I had a similar problem using axes. The class parameter is frameon but the kwarg is frame_on. axes_api >>> plt.gca().set(frameon=False) AttributeError: Unknown property frameon
Solution
frame_on
Example
data = range(100)
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(data)
#ax.set(frameon=False) # Old
ax.set(frame_on=False) # New
plt.show()