mishaF
mishaF

Reputation: 8314

How can I make a blank subplot in matplotlib?

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?

Upvotes: 134

Views: 124018

Answers (6)

user467491
user467491

Reputation: 21

To delete the the plot positioned at (2,1) you may use

import matplotlib.pyplot as plt
cfig,ax = plt.subplots(3,2)
cfig.delaxes(ax.flatten()[5])

Upvotes: 2

mins
mins

Reputation: 7494

With newer versions of matplotlib (since 3.4.0), you can use an array of strings to design your subplots pattern (rows and columns) and use it to create the figure by calling matplotlib.pyplot.subplot_mosaic.

Instead of cfig, ax = plt.subplots(3,2), use cfig, axs = plt.subplot_mosaic(mosaic) and define mosaic this way:

mosaic = [['a', 'b'],
          ['c', 'd'],
          ['e', '.']]

In this pattern, blank subplots are denoted by '.' (by default, this can be parametrized in the call). You do not need to delete blank subplots, as they are not even created.

To select an axis for plotting, just use axs[id] where id is the string used to identify the subplot in the mosaic array.

Example:

mosaic = [['b', 'a'], ['.', 'au']]
kw = dict(layout='constrained')
fig, axs = plt.subplot_mosaic(mosaic, **kw)

ax = axs['b']
ax.grid(axis='y')
ax.bar(n, d)

ax = axs['a']
ax.grid(axis='y')
ax.bar(n, prior)

[...]

enter image description here

With subplot_mosaic, not only you can introduce blank subplots, but you can also merge 'cells' in order to create subplots on multiple rows and/or columns, just by crafting the required mosaic array, the rest of the code is unchanged. In addtion mosaic doesn't need to be an array, it can also be a multiline string. E.g. from Complex and semantic figure composition, using:

"""
A.C
BBB
.D.
"""

results in:

enter image description here

Upvotes: 3

Nick Hunkins
Nick Hunkins

Reputation: 179

It's also possible to hide a subplot using the Axes.set_visible() method.

import matplotlib.pyplot as plt
import pandas as pd

fig = plt.figure()
data = pd.read_csv('sampledata.csv')

for i in range(0,6):
    ax = fig.add_subplot(3,2,i+1)
    ax.plot(range(1,6), data[i])
    if i == 5:
        ax.set_visible(False)

Upvotes: 17

Chris
Chris

Reputation: 46306

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:

An image of a 3x2 grid of graphs, with no graph rendered in the bottom right cell

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

Upvotes: 229

Hooked
Hooked

Reputation: 88118

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

enter image description here

Upvotes: 31

moooeeeep
moooeeeep

Reputation: 32502

Would it be an option to create the subplots when you need them?

import matplotlib
matplotlib.use("pdf")
import matplotlib.pyplot as plt

plt.figure()
plt.gcf().add_subplot(421)
plt.fill([0,0,1,1],[0,1,1,0])
plt.gcf().add_subplot(422)
plt.fill([0,0,1,1],[0,1,1,0])
plt.gcf().add_subplot(423)
plt.fill([0,0,1,1],[0,1,1,0])
plt.suptitle("Figure Title")
plt.gcf().subplots_adjust(hspace=0.5,wspace=0.5)
plt.savefig("outfig")

Upvotes: 2

Related Questions