13

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
  • 7,560
  • 14
  • 42
  • 51

3 Answers3

24

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
  • 24,919
  • 11
  • 68
  • 88
12

A simpler way:

import matplotlib.pyplot as plt
plt.tick_params(left=False, labelleft=False) #remove ticks
plt.box(False) #remove box
neves
  • 33,186
  • 27
  • 159
  • 192
11

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
  • 18,065
  • 5
  • 46
  • 67