Skip to content Skip to sidebar Skip to footer

Turn Axes Off For All Subplots Of A Figure

I am creating a large array of subplots and I want to turn off axes for all the subplots. Currently I am achieving this by fig, ax = plt.subplots(7, len(clusters)) fig.subplots_adj

Solution 1:

I agree with @tcaswell that you should probably just use what you're already using. Another option to use it as a function is to use numpy.vectorize():

import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots(7, len(clusters))
np.vectorize(lambda ax:ax.axis('off'))(ax)

or, if you need to invoke it multiple times, by assigning the vectorized function to a variable:

axoff_fun = np.vectorize(lambda ax:ax.axis('off'))
# ... stuff here ...
fig, ax = plt.subplots(7, len(clusters))
axoff_fun(ax)

Again, note that this is the same thing that @tcaswell suggested, in a fancier setting (only slower, probably). And it's essentially the same thing you're using now.


However, if you insist on doing it some other way (i.e. you are a special kind of lazy), you can set matplotlib.rcParams once, and then every subsequent axes will automatically be off. There's probably an easier way to emulate axis('off'), but here's how I've succeeded:

import matplotlib as mpl

# before
mpl.pyplot.figure()
mpl.pyplot.plot([1,3,5],[4,6,5])

# kill axis in rcParams
mpl.rc('axes.spines',top=False,bottom=False,left=False,right=False);
mpl.rc('axes',facecolor=(1,1,1,0),edgecolor=(1,1,1,0));
mpl.rc(('xtick','ytick'),color=(1,1,1,0));

# after
mpl.pyplot.figure()
mpl.pyplot.plot([1,3,5],[4,6,5])

Result before/after:

before rcparamsafter rcparams

Hopefully there aren't any surprises which I forgot to override, but that would become clear quite quickly in an actual application anyway.

Post a Comment for "Turn Axes Off For All Subplots Of A Figure"