0

I've plotted a graph containing many subplots, by using matplotlib in Python 3. I inserted the figure title by using fig.text() rather than fig.suptitle() because I need to place the title in the middle, on the right, and on the left. You can see this result in the next url (number of rows = 6):

RESULT OF PLOT: N-rows = 6

Now, I have some problems when the number of subplots increase or decrease. The space between the title and the top of the upper subplots decrease or increase. The x label has the same behavior as well.
For instance, when number of rows = 2 that space decrease, as you can see in the next picture:

RESULT OF PLOT: N-rows = 2

In the other hand, when number of rows = 15 that space increase, as you can see in the next picture:

RESULT OF PLOT: N-rows = 15

I want that the space of the title and the x axis label don't change when I increase or decrease the number of rows. Any suggestion? Thank you.

This is my MWE:

import matplotlib.pyplot as plt
 
n_rows = 15
n_cols = 2
fig, axs = plt.subplots(n_rows, n_cols, figsize=(7*n_cols,1*n_rows), sharex="col", sharey="row",
                        gridspec_kw={'hspace': 0, 'wspace': 0})  
# labels
fig.text(0.513, 0.06, 'Time UTC', ha='center', va='center', fontsize=14)
fig.text(0.09, 0.5, 'S4', ha='center', va='center', rotation='vertical', fontsize=14, color='b')
fig.text(0.94, 0.5, 'Elevation Angle', ha='center', va='center', rotation=-90, fontsize=14, color='g')
# title
fig.text(0.513, 0.91, 'S4', ha='center', va='center', fontsize=17)
fig.text(0.32, 0.91, 'Jicamarca', ha='center', va='center', fontsize=17, color='r')
fig.text(0.12, 0.91, '2020/07/16', ha='left', va='center', fontsize=17)
fig.text(0.9, 0.91, 'Scintillation index', ha='right', va='center', fontsize=17)
  • Clearly, the hard-coded `y` values (also x) for text positioning are bad when number of `rows` is small. You need some formula to set (x,y) of the texts. – swatchai Dec 02 '20 at 00:35
  • For the figure's title, try this: `fig.suptitle("Super Title", va='bottom')`. – swatchai Dec 02 '20 at 00:44
  • @swatchai, I was also thinking about inserting a formula for the texts. But, isn't there any other simpler option? for example, placing the title always above the first subplot by keeping a certain distance between them. – Luis D. Dec 02 '20 at 16:55

0 Answers0