Python & Matplotlib: creating two subheadings of different sizes

I have a script that creates one or two diagrams, depending on whether a particular condition is fulfilled or not. In fact, basically, I still do 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 relevant data, but this simple version that I made is executable anyway) generates two subheadings of the same size, one above the other. However, I would like to get the following:

  • If somecondition is True , then both subheadings should appear in the figure. Therefore, I would like the second subtitle to be 1/2 less than the first;
  • somecondition False, , , ( , ).

, , , 211 212 ( , , Python ). - , , , ? , , , , ? !

+4
2

?

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

, , matplotlib.gridspec,

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

+5

, :

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)

: nrows, ncols, plot_number, . . , 3,1,3 3 , 1 . - 313.

plot_number, , : 3,1,(1,2).

+5

All Articles