Skip to content
Advertisement

Axis labels are cut off when using subplot with just one plot

I have plot with axis labels looking like this

subplot(111)

created by this code:

import matplotlib.pyplot as plt

fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.set_xlabel('x-label 1')
ax1.set_ylabel('y-label 1')

plt.show()

If I resize the master object of the plot (in this case the window) to smaller size, the axes labels are cut off like so

height cut off

and

width cut off

but if I create the subplot with two plots (the second one doesn’t have to be populated with a plot), the labels are not cut off, even though the plot is smaller:

subplot(211)

and

subplot(121)

How do I prevent the labels from being cut off when using add_subplot(111) and resizing its master?

Advertisement

Answer

You should use a tight or constrained layout by setting for instance

fig = plt.figure(layout='constrained')

See Constrained Layout Guide for more details.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement