Skip to content Skip to sidebar Skip to footer

Remove (sub)plot, But Keep Axis Label In Matplotlib

I want to create a subplot plot in matplotlib with, say, 2 rows and 2 columns, but I only have 3 things to plot and want to keep the lower left subplot empty. However, I still want

Solution 1:

Unfortunately you will need to remove the elements of the axis individually in order to keep the ylabel, because the ylabel is itself also an element of the axis.

import matplotlib.pyplot as plt

fig, axes = plt.subplots(2,2)
fig.set_facecolor("#ecfaff")
for i, ax in enumerate(axes.flatten()):
    if i!=2:
        ax.plot([3,4,6])
    if not i%2:
        ax.set_ylabel("My label")

# make xaxis invisibel
axes[1,0].xaxis.set_visible(False)
# make spines (the box) invisible
plt.setp(axes[1,0].spines.values(), visible=False)
# remove ticks and labels for the left axis
axes[1,0].tick_params(left=False, labelleft=False)
#remove background patch (only needed for non-white background)
axes[1,0].patch.set_visible(False)

plt.show()

enter image description here


Solution 2:

A cleaner way:

plt.gca().set_yticklabels([])
plt.gca().set_xticklabels([])
plt.gca().set_xticks([])
plt.gca().set_yticks([])

Post a Comment for "Remove (sub)plot, But Keep Axis Label In Matplotlib"