2

I'm very new to Python, and I want to plot 13 different figures all in one plot. To do this nicely, I would like to plot the first 12 figures in a 6x2 grid (this works just fine), and then plot the 13th figure below it; either the same size as the other figures and centered, or larger than the rest so that its width is equal to twice the width of the other figures and all the edges are aligned. What would be the best way to specify axes of this kind using subplots? (So far, I've just used nrows=6, ncols=2, but I think something like that won't work with an odd number of figures to plot.) The code I have so far for plotting the first 12 plots looks like this (with simple test data):

fig, axes = plt.subplots(nrows=6, ncols=2, figsize=(45,10))
for ax in axes.flat:
   ax.plot([1,2,3,4])
fig.subplots_adjust(right=0.5)

How can I add a 13th figure below the others?

1

1 Answer 1

4

You can use GridSpec (link to documentation) to generate flexible axes layout.

The following code creates the desired layout and puts all Axes objects in a list for easy access.

gs00 = matplotlib.gridspec.GridSpec(7, 2)

fig = plt.figure()
axs = []
for i in range(6):
    for j in range(2):
        ax = fig.add_subplot(gs00[i,j])
        axs.append(ax)
ax = fig.add_subplot(gs00[6,:])
axs.append(ax)

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.