Is it possible to add subplots incrementally inside a loop instead of setting the subplots shape at the beginning?

Viewed 86

First of all, the shape of the subplots will be (n,1), so additional subplots should be "appended" at the bottom of the figure as needed.

I would like to do a large number of loops over my raw data, and inside each loop I would like to:

  1. generate a dataframe in each loop,
  2. check each dataframe for whether it needs to be plotted
  3. if the dataframe is to be plotted, add a subplot to the figure.

I don't know how to add subplots to a figure incrementally as needed.

It is certainly possible for me to get all the data first and determine the number of subplots needed and set the figsize first before plotting everything at once. But this is not ideal for me.

1 Answers

May be this could help you:

 import matplotlib.pyplot as plt


plt.ion()
fig = plt.figure()

ax = fig.subplots(1, 1)
plt.scatter([1, 2, 3], [1, 2, 3])

while True:
    plt.waitforbuttonpress()
    break

plt.clf()

ax = fig.subplots(2, 1)
ax[0].scatter([1, 2, 3], [1, 2, 3])

Once first plot appears, hit a key on the keyboard (or press mouse button).

Related