I am making a group of subplot (say, 3 x 2) in matplotlib, but I have fewer than 6 datasets. How can I make the remaining subplot blank?
The arrangement looks like this:
+----+----+ | 0,0| 0,1| +----+----+ | 1,0| 1,1| +----+----+ | 2,0| 2,1| +----+----+
This may go on for several pages, but on the final page, there are, for example, 5 datasets to the 2,1 box will be empty. However, I have declared the figure as:
cfig,ax = plt.subplots(3,2)
So in the space for subplot 2,1 there is a default set of axes with ticks and labels. How can I programatically render that space blank and devoid of axes?
We can turn off the axes of subplots in Matplotlib using axis() and set_axis_off() methods for axes objects. We can also turn off axes using the axis() method for the pyplot object. To turn off axis for X-axis in particular we use axes.
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.
To create multiple plots use matplotlib. pyplot. subplots method which returns the figure along with Axes object or array of Axes object. nrows, ncols attributes of subplots() method determine the number of rows and columns of the subplot grid.
You could always hide the axes which you do not need. For example, the following code turns off the 6th axes completely:
import matplotlib.pyplot as plt hf, ha = plt.subplots(3,2) ha[-1, -1].axis('off') plt.show()
and results in the following figure:
Alternatively, see the accepted answer to the question Hiding axis text in matplotlib plots for a way of keeping the axes but hiding all the axes decorations (e.g. the tick marks and labels).
A much improved subplot interface has been added to matplotlib since this question was first asked. Here you can create exactly the subplots you need without hiding the extras. In addition, the subplots can span additional rows or columns.
import pylab as plt ax1 = plt.subplot2grid((3,2),(0, 0)) ax2 = plt.subplot2grid((3,2),(0, 1)) ax3 = plt.subplot2grid((3,2),(1, 0)) ax4 = plt.subplot2grid((3,2),(1, 1)) ax5 = plt.subplot2grid((3,2),(2, 0)) 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