Setting spines in matplotlibrc
To make matplotlib not to draw upper and right spines, one can set the following in matplotlibrc file
axes.spines.right : False
axes.spines.top : False
In order to hide the right and top spines of a subplot, you need to both set the colour of the relevant spines to 'none'
, as well as set the tick position to 'left'
for the xtick, and 'bottom'
for the ytick (in order to hide the tick marks as well as the spines).
Unfortunately, none of these are currently accessible via matplotlibrc
. The parameters specified in matplotlibrc
are validated, and then stored in a dict called rcParams
. It is then up to the individual modules to check for a key in this dict whose value will act as their default. If they don't check it for one of their options, that option is not alterable via the rc
file.
Due to the nature of the rc
system, and the way that spines are written, altering the code to allow for this would not be straightforward:
Spines currently obtain their colour through the same rc
parameter used to define axis colours; you cannot set it to 'none'
without hiding all of your axis drawing. They are also agnostic towards whether they are top
, right
, left
, or bottom
— these are really just four separate spines stored in a dict. The individual spine objects do not know what side of the plot they compose, so you cannot just add new rc
params and assign the proper one during spine initialization.
self.set_edgecolor( rcParams['axes.edgecolor'] )
(./matplotlib/lib/matplotlib/spines.py, __init__(), line 54)
If you have a large amount of existing code, such that adding the axis parameters manually to each one would be too burdensome, you could alternately use a helper function to iterate through all of the Axis objects and set the values for you.
Here's an example:
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.pyplot import show
# Set up a default, sample figure.
fig = plt.figure()
x = np.linspace(-np.pi,np.pi,100)
y = 2*np.sin(x)
ax = fig.add_subplot(1,2,2)
ax.plot(x,y)
ax.set_title('Normal Spines')
def hide_spines():
"""Hides the top and rightmost axis spines from view for all active
figures and their respective axes."""
# Retrieve a list of all current figures.
figures = [x for x in matplotlib._pylab_helpers.Gcf.get_all_fig_managers()]
for figure in figures:
# Get all Axis instances related to the figure.
for ax in figure.canvas.figure.get_axes():
# Disable spines.
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
# Disable ticks.
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
hide_spines()
show()
Just call hide_spines()
before show()
, and it will hide them in all of the figures that show()
displays. I cannot think of a simpler way to alter a large number of figures, outside of spending the time to patch matplotlib
and add in rc
support for the needed options.