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.
Matplotlib removes both labels and ticks by using xticks([]) and yticks([]) By using the method xticks() and yticks() you can disable the ticks and tick labels from both the x-axis and y-axis.
Subplots mean groups of axes that can exist in a single matplotlib figure. subplots() function in the matplotlib library, helps in creating multiple layouts of subplots. It provides control over all the individual plots that are created.
You have the right method. Maybe you are not applying the set_xticks
to the correct axes.
An example:
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([])
This gives:
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.
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=[]);
Note: this must be called before any calls to plt.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With