Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Borderless matplotlib plots

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.

like image 304
Osmond Bishop Avatar asked Sep 04 '13 01:09

Osmond Bishop


3 Answers

A simpler way:

import matplotlib.pyplot as plt
plt.tick_params(left=False, labelleft=False) #remove ticks
plt.box(False) #remove box
like image 116
neves Avatar answered Oct 10 '22 03:10

neves


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

like image 27
Phillip Cloud Avatar answered Oct 10 '22 01:10

Phillip Cloud


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()]
like image 10
wflynny Avatar answered Oct 10 '22 03:10

wflynny