Remove the extra plot in the matplotlib subplot
Alternatively, using axes
method set_axis_off()
:
axes[1,2].set_axis_off()
Try this:
fig.delaxes(axes[1][2])
A much more flexible way to create subplots is the fig.add_axes()
method. The parameters is a list of rect coordinates: fig.add_axes([x, y, xsize, ysize])
. The values are relative to the canvas size, so an xsize
of 0.5
means the subplot has half the width of the window.
If you know which plot to remove, you can give the index and remove like this:
axes.flat[-1].set_visible(False) # to remove last plot
Turn off all axes, and turn them on one-by-one only when you're plotting on them. Then you don't need to know the index ahead of time, e.g.:
import matplotlib.pyplot as plt
columns = ["a", "b", "c", "d"]
fig, axes = plt.subplots(nrows=len(columns))
for ax in axes:
ax.set_axis_off()
for c, ax in zip(columns, axes):
if c == "d":
print("I didn't actually need 'd'")
continue
ax.set_axis_on()
ax.set_title(c)
plt.tight_layout()
plt.show()