10

Is there a way to save matplotlib graphs without the border around the frame while keeping the background not transparent?

Setting the frame to 'off' as I show in the code below does not work as this removes the background making it transparent whereas I want to retain the white background, just without the borders.

a = fig.gca()  
a.set_frame_on(False)  

Here is a screenshot of what I'm trying to do. If the border can be removed then I can draw the x-axis line separately.

enter image description here

All suggestions are much appreciated.

Osmond Bishop
  • 6,430
  • 11
  • 39
  • 49
  • I'm curious as to why you accepted Bill's answer...I gave the same answer 1 minute before he did. It's not a big deal, I'm just wondering if he did something that I didn't. – Phillip Cloud Sep 04 '13 at 02:12
  • As I scrolled down the page it was the first answer that appeared and it worked. I've accepted yours as your answer was earlier. – Osmond Bishop Sep 04 '13 at 02:27

3 Answers3

19

A similar question was asked here: How can I remove the top and right axis in matplotlib?. A Google search for "hide axes matplotlib" gives that as the 5th link.

Remove the spines:

x = linspace(0, 2 * pi, 1000)
y = sin(x)
fig, ax = subplots()
ax.plot(x, y)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.grid(axis='y')

enter image description here

Community
  • 1
  • 1
Phillip Cloud
  • 23,488
  • 11
  • 67
  • 88
9

A simpler way:

import matplotlib.pyplot as plt
plt.tick_params(left=False, labelleft=False) #remove ticks
plt.box(False) #remove box
neves
  • 26,235
  • 24
  • 129
  • 157
8

You can try to use ax.spines. For example:

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)

ax.plot(x, y)

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

If you want to remove all spines (and probably ticks as well), you can do

[s.set_visible(False) for s in ax.spines.values()]
[t.set_visible(False) for t in ax.get_xticklines()]
[t.set_visible(False) for t in ax.get_yticklines()]
wflynny
  • 17,180
  • 5
  • 41
  • 60