Spines in Matplotlib refer to the lines surrounding the axes of the plotting area. We can set each spine to have different line styles or to be invisible.
To begin, we first access the axes with plt.gca(), where gca stands for get current axes, and store it in a variable, say ax.
We then adjust the properties of each spine in ax.spines with either 'top', 'right', 'bottom', or 'left'. The common settings include line widths, color, and visibility, which is demonstrated in the following code snippet.
The following is an example to remove the top and right spines, often seen as a convention in certain scientific plots, and to enhance visual simplicity in general. It is also common to thicken the remaining spines. The change in color is shown as a demonstration. We can adjust it to suit our overall design where the plot is displayed:
import matplotlib.pyplot as plt
y = list(range(4))
plt.plot(y)
# Store the current axes as ax
ax = plt.gca()
# Set the spine properties
ax.spines['left'].set_linewidth(3)
ax.spines['bottom'].set_linewidth(3)
ax.spines['left'].set_color('darkblue')
ax.spines['bottom'].set_color('darkblue')
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
plt.show()
This will create a graph of blue spines on the left and bottom: