257

To remove frame in figure, I write

frameon=False

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.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Emmet B
  • 5,341
  • 6
  • 34
  • 47
  • Can you clarify what you're doing? (i.e. show an example) Are you using `savefig`? (If so, it overrides whatever you set when saving the figure.) Does manually setting `fig.patch.set_visible(False)` work? – Joe Kington Feb 16 '13 at 17:31
  • I use canvas.print_png(response), not savefig. – Emmet B Feb 16 '13 at 17:48

11 Answers11

394

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:

ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)

And, in case of removing x and y ticks from the plot:

 ax.get_xaxis().set_ticks([])
 ax.get_yaxis().set_ticks([])
Sanchit
  • 3,180
  • 8
  • 37
  • 53
igr
  • 4,529
  • 2
  • 15
  • 20
  • 6
    Just to add: When you also want to remove the ticks: `ax.yaxis.set_ticks_position('left') ax.xaxis.set_ticks_position('bottom')` – JLT Jan 05 '17 at 13:01
  • 8
    Also, use ax=gca() to create an axis object if you don't already have one. – cameronroytaylor Mar 31 '17 at 19:45
  • 3
    This should be the accepted answer. "ax.axis("off")" removes everything like: x and y labels, x and y ticks and all (4) borders from the plot. For better customisation, each element should be handled differently. – Sanchit Nov 09 '17 at 11:25
219

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)

enter image description here

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

enter image description here

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)

enter image description here

Joe Kington
  • 275,208
  • 71
  • 604
  • 463
  • so this solves half the problem. But I also want this black frame rectangle to be invisible. So only the blue line should be visible. – Emmet B Feb 16 '13 at 18:07
  • 6
    Oh, well, in that case it's even simpler. Just use `ax.axis('off')` (you'll still need to turn the figure frame off as well). – Joe Kington Feb 16 '13 at 20:18
  • thanks, is there a way to keep ticklabels, such as: I want only labels `ax.set_yticklabels(('G1', 'G2', 'G3'))` – Emmet B Feb 17 '13 at 09:43
  • This is great, I used it for another application here: http://stackoverflow.com/questions/4092927/generating-movie-from-python-without-saving-individual-frames-to-files/29525514#29525514 – cxrodgers Apr 08 '15 at 21:19
  • 2
    The `print_png()` throws a `TypeError: write() argument must be str, not bytes` exception for me on python 3. Opening the file as write binary (`'wb'`) is needed for it to work. – Gabriel Jablonski Nov 03 '19 at 22:18
130

The easiest way to get rid of the ugly frame in newer versions of matplotlib:

import matplotlib.pyplot as plt
plt.box(False)

If you really must always use the object-oriented approach, then do: ax.set_frame_on(False).

neves
  • 33,186
  • 27
  • 159
  • 192
  • 7
    When using the object oriented approach, I had success with ```ax.set_frame_on(False)```, which does the same thing. Maybe include in the above answer. – Floyd4K Oct 01 '19 at 16:15
  • 2
    This removes the entire box/rectangle, not just the frame/borders. May or may not be what you want. – getup8 Jan 25 '21 at 06:18
  • And to only hide the top and right lines, the solution is [here](https://stackoverflow.com/q/925024/774575). – mins Oct 21 '22 at 09:29
70

Building up on @peeol's excellent answer, you can also remove the frame by doing

for spine in plt.gca().spines.values():
    spine.set_visible(False)

To give an example (the entire code sample can be found at the end of this post), let's say you have a bar plot like this,

enter image description here

you can remove the frame with the commands above and then either keep the x- and ytick labels (plot not shown) or remove them as well doing

plt.tick_params(top='off', bottom='off', left='off', right='off', labelleft='off', labelbottom='on')

In this case, one can then label the bars directly; the final plot could look like this (code can be found below):

enter image description here

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()
Cleb
  • 25,102
  • 20
  • 116
  • 151
14

As I answered here, you can remove spines from all your plots through style settings (style sheet or rcParams):

import matplotlib as mpl

mpl.rcParams['axes.spines.left'] = False
mpl.rcParams['axes.spines.right'] = False
mpl.rcParams['axes.spines.top'] = False
mpl.rcParams['axes.spines.bottom'] = False
hhh
  • 1,913
  • 3
  • 27
  • 35
13

Problem

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()
A. West
  • 571
  • 5
  • 12
9
df = pd.DataFrame({
'client_scripting_ms' : client_scripting_ms,
 'apimlayer' : apimlayer, 'server' : server
}, index = index)

ax = df.plot(kind = 'barh', 
     stacked = True,
     title = "Chart",
     width = 0.20, 
     align='center', 
     figsize=(7,5))

plt.legend(loc='upper right', frameon=True)

ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)

ax.yaxis.set_ticks_position('left')
ax.xaxis.set_ticks_position('right')
petezurich
  • 9,280
  • 9
  • 43
  • 57
Taha Kaiyum
  • 99
  • 1
  • 2
5

I use to do so:

from pylab import *
axes(frameon = 0)
...
show()
alinsoar
  • 15,386
  • 4
  • 57
  • 74
5
plt.axis('off')
plt.savefig(file_path, bbox_inches="tight", pad_inches = 0)

plt.savefig has those options in itself, just need to set axes off before

mohammad ess
  • 51
  • 1
  • 1
3
plt.box(False)
plt.xticks([])
plt.yticks([])
plt.savefig('fig.png')

should do the trick.

user3496912
  • 195
  • 1
  • 6
2

here is another solution :

img = io.imread(crt_path)

fig = plt.figure()
fig.set_size_inches(img.shape[1]/img.shape[0], 1, forward=False) # normalize the initial size
ax = plt.Axes(fig, [0., 0., 1., 1.]) # remove the edges
ax.set_axis_off() # remove the axis
fig.add_axes(ax)

ax.imshow(img)

plt.savefig(file_name+'.png', dpi=img.shape[0]) # de-normalize to retrieve the original size