Novel
Novel

Reputation: 13729

Matplotlib subplots are all the same size ... how to set them up so subplots scale including the xticks?

Here's some example code:

from matplotlib import pyplot as plt

plt.subplot(221)
plt.bar(range(3),[34, 37, 16]) # some random data
plt.xticks(range(3),"Jan,Feb,Mar".split(','))

plt.subplot(222)
plt.bar(range(3),[34, 37, 16])
plt.xticks(range(3),"January,February,March".split(','), rotation=90)

plt.subplot(223)
plt.bar(range(3),[34, 37, 16])
plt.xticks(range(3),"Jan,Feb,Mar".split(','))

plt.tight_layout()
plt.show()

Which results in this, with all the plot heights equal size:

enter image description here

But I'd like it to look like this, with the entire 'subfigure' equal size:

enter image description here

Upvotes: 0

Views: 260

Answers (1)

Jody Klymak
Jody Klymak

Reputation: 5913

Using subfigures (https://matplotlib.org/stable/gallery/subplots_axes_and_figures/subfigures.html), you can create 4 virtual places to draw on your plot but they don't try to align with each other using tight or constrained_layout (note tight_layout doesn't work with subfigures).

from matplotlib import pyplot as plt

fig = plt.figure(constrained_layout=True)

sfigs = fig.subfigures(2, 2)

ax = sfigs[0, 0].subplots()
ax.bar(range(3),[34, 37, 16]) # some random data
ax.set_xticks(range(3),"Jan,Feb,Mar".split(','))

ax = sfigs[0, 1].subplots()
ax.bar(range(3),[34, 37, 16])
ax.set_xticks(range(3),"January,February,March".split(','), rotation=90)

ax = sfigs[1, 0].subplots()
ax.bar(range(3),[34, 37, 16])
ax.set_xticks(range(3),"Jan,Feb,Mar".split(','))

plt.show()

enter image description here

Upvotes: 1

Related Questions