Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 828
Palmetto_Girl86 Avatar asked Aug 04 '14 17:08

Palmetto_Girl86


People also ask

How do I get rid of tick marks in MatPlotLib?

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.

What does subplots () do in MatPlotLib?

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.


2 Answers

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:

enter image description here

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.

like image 65
DrV Avatar answered Sep 30 '22 21:09

DrV


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()

like image 22
Roy Shilkrot Avatar answered Sep 30 '22 20:09

Roy Shilkrot