Matplotlib Subplots -- Get Rid of Tick Labels Altogether
Is there a way to get rid of tick labels altogether when creating an array of subplots in Matplotlib? I am currently needing to specify each plot based on the row and column of a larger data set to which the plot corresponds. I've attempted to use the ax.set_xticks() and the similar y-axis command, to no avail.
I recognize that it's probably an unusual request to want to make a plot with no axis data whatsoever, but that's what I need. And I need it to automatically apply to all of the subplots in the array.
You have the right method. Maybe you are not applying the set_xticks to the correct axes.
import matplotlib.pyplot as plt import numpy as np ncols = 5 nrows = 3 # create the plots fig = plt.figure() axes = [ fig.add_subplot(nrows, ncols, r * ncols + c) for r in range(0, nrows) for c in range(0, ncols) ] # add some data for ax in axes: ax.plot(np.random.random(10), np.random.random(10), '.') # remove the x and y ticks for ax in axes: ax.set_xticks() ax.set_yticks()
Note that each axis instance is stored in a list (axes) and then they can be easily manipulated. As usual, there are several ways of doing this, this is just an example.
The commands are the same for subplots
In : fig = plt.figure() In : ax1 = fig.add_subplot(211) In : ax2 = fig.add_subplot(212) In : ax1.plot([1,2]) Out: [<matplotlib.lines.Line2D at 0x10ce9e410>] In : ax1.tick_params( ....: axis='x', # changes apply to the x-axis ....: which='both', # both major and minor ticks are affected ....: bottom='off', # ticks along the bottom edge are off ....: top='off', # ticks along the top edge are off ....: labelbottom='off' # labels along the bottom edge are off) ....:) In : plt.draw()
Even more concise than @DrV 's answer, remixing @mwaskom's comment, a complete and total one-liner to get rid of all axes in all subplots:
# do some plotting... plt.subplot(121),plt.imshow(image1) plt.subplot(122),plt.imshow(image2) # .... # one liner to remove *all axes in all subplots* plt.setp(plt.gcf().get_axes(), xticks=, yticks=);