Creating two subplots with different sizes

Question:

I have a script which is creating one or two charts, depending on if one specific condition is met or not. Really basically, what I am doing so far is the following:

import matplotlib.pyplot as plt

list1 = [1,2,3,4]
list2 = [4,3,2,1]
somecondition = True
plt.figure(1) #create one of the figures that must appear with the chart
ax = plt.subplot(211) #create the first subplot that will ALWAYS be there
ax.plot(list1) #populate the "main" subplot
if somecondition == True:
   ax = plt.subplot(212) #create the second subplot, that MIGHT be there
   ax.plot(list2) #populate the second subplot
plt.show()

This code (with the proper data, but this simple version that I did is executable anyway) generates two subplots of the same size, one above the other. However, what I would like to get is the following:

  • If somecondition is True, then both subplots should appear in the figure. Hence, I would like the second subplot to be 1/2 smaller than the first one;
  • If somecondition is False, then just the first subplot should appear and I would like it to be sized as the all figure (without leaving the empty space behind in the case the second subplot will not appear).

I’m pretty sure it’s just a matter of sizing the two subplots, probably even by the parameter 211 and 212 (that I don’t understand what they stand for, since I’m new to Python and couldn’t find a clear explanation on the web yet). Does anyone know how to regulate the size of the subplots in a easy way, proportionally to the number of subplots as well as to the entire size of the figure? To make it easier to understand, could you also please edit my simple code I attached to get the result I’m looking for? Thanks in advance!

Asked By: Matteo NNZ

||

Answers:

does this solution satisfy?

import matplotlib.pyplot as plt

list1 = [1,2,3,4]
list2 = [4,3,2,1]
somecondition = True
plt.figure(1) #create one of the figures that must appear with the chart

if not somecondition:
    ax = plt.subplot(111) #create the first subplot that will ALWAYS be there
    ax.plot(list1) #populate the "main" subplot
else:
    ax = plt.subplot(211)
    ax.plot(list1)
    ax = plt.subplot(223) #create the second subplot, that MIGHT be there
    ax.plot(list2) #populate the second subplot
plt.show()

enter image description here

If you need the same width but with half height, better to use matplotlib.gridspec, reference here

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

list1 = [1,2,3,4]
list2 = [4,3,2,1]
somecondition = True
plt.figure(1) #create one of the figures that must appear with the chart

gs = gridspec.GridSpec(3,1)

if not somecondition:
    ax = plt.subplot(gs[:,:]) #create the first subplot that will ALWAYS be there
    ax.plot(list1) #populate the "main" subplot
else:
    ax = plt.subplot(gs[:2, :])
    ax.plot(list1)
    ax = plt.subplot(gs[2, :]) #create the second subplot, that MIGHT be there
    ax.plot(list2) #populate the second subplot
plt.show()

enter image description here

Answered By: zhangxaochen

It seems you are looking for this:

if somecondition:
    ax = plt.subplot(3,1,(1,2))
    ax.plot(list1)
    ax = plt.subplot(3,1,3)
    ax.plot(list2)
else:
    plt.plot(list1)

The magic numbers are nrows, ncols, plot_number, see the documentation. So 3,1,3 will create 3 rows, 1 column, and will plot into the third cell. An abbreviation for that is 313.

It’s possible to use tuple as plot_number, so you can create a plot which lives in the first and second cell: 3,1,(1,2).

Answered By: Norbert Sebők
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.