0

I have Python code similar to the example below to remove the top and right axes of plots created with Matplotlib. The tick marks are also removed for all plot axes.

import matplotlib.pyplot as plt
plt.close('all')

plt.figure(1)
plt.plot(x1, y1)
plt.grid()

plt.figure(2)
plt.plot(x2, y2)
plt.grid()

plt.figure(3)
plt.plot(x3, y3)
plt.grid()

ax = plt.gca()
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
plt.tick_params(axis='both', bottom='off', top='off', left='off', right='off')

plt.show()

Unfortunately, the desired effect is only applied to the last plot figure. To remove the top/right axes and tick marks for all figures, I have to place the following code after each figure:

ax = py.gca()
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
py.tick_params(...)

Is it possible to customize the axes of all figures at once using Matplotlib? I don't want to remove the axes for just one figure, I want to remove the top and right axes on all the figures without having to write .set_visible() many times.

wigging
  • 8,492
  • 12
  • 75
  • 117
  • Why do you not want to apply `.set_visible(False)` to each axes? – Joooeey May 06 '23 at 16:15
  • Because it only works for one figure. Is there a way to call `.set_visible()` once and remove certain axes for all the figures? – wigging May 06 '23 at 18:05
  • Yes, you can use Matplotlib's explicit API: https://matplotlib.org/stable/api/index.html#the-explicit-api – Joooeey May 06 '23 at 18:16
  • Please edit in the appropriate import statement! I assume it's `import matplotlib.pyplot as py`? – Joooeey May 06 '23 at 18:19

2 Answers2

2

At least the current version of Matplotlib also supports this through the matplotlibrc config file:

axes.spines.top    : False
axes.spines.right  : False 

Or, using the rcParams:

import matplotlib as mpl
mpl.rcParams["axes.spines.right"] = False
mpl.rcParams["axes.spines.top"] = False

Depending on your configuration, you might also have to remove the ticks at the top/right axis (xtick.top : False, ytick.right : False).

Bart
  • 9,825
  • 5
  • 47
  • 73
0

Sure you can! The manual way is by setting matplotlib's rcParams. For example:

import matplotlib as mpl
mpl.rcParams["axes.labelsize"] = 12

You need to do this before plotting.

See the docs here.

That being said, I highly recommend seaborn to customize your plots. At least that will give you a head start. You can further customize them at will using rcParams.

jorgeh
  • 1,727
  • 20
  • 32
  • I looked at `rcParams` but there is not a way to hide the top and right axes or turn off all the tick marks. – wigging Nov 16 '15 at 21:28
  • Seaborn's `despine()` method (see [this link](http://stanford.edu/~mwaskom/software/seaborn/generated/seaborn.despine.html#seaborn.despine) turns off top and right axes. In the same link (see "overriding elements of the searborn styles" section), it explains how to make tick marks invisible (basically, set their size to 0) – jorgeh Nov 16 '15 at 22:34
  • Is there a way to do this without using Seaborn? – wigging Nov 17 '15 at 16:57
  • Of course, but I don't know from the top of my head. I'd look at seaborn's source code to see how they do it. A quick search suggests that what you are looking for is in the [utils module](https://github.com/mwaskom/seaborn/blob/master/seaborn/utils.py) (look for the `despine()` function) – jorgeh Nov 17 '15 at 21:54