28

So far i have placed my suptitles above the frame, like this:

enter image description here

How can i get the suptitles from above the frame into the frame?

So far i have a solution that just prints a text and sets it on the right position with computing xlim and ylim. However this is errorprone and if the text is different it just looks aweful. Is there a way to set the suplabel into the frame? Or just place text below the frame and centered? it would be really convenient, if i did not need to know about the data that is displayed inside the frame.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
tarrasch
  • 2,630
  • 8
  • 37
  • 61

2 Answers2

45

Your solution using text is also my go-to solution. However, you don't need to compute the position based on xlim and ylim. If you set transform=ax.transAxes the coordinates for positioning the text are taken as being relative to the axes bounding box (0,0 being the lower left corner). Like so:

data = range(1,10);
fig = figure()
for i in range(6):
    ax = fig.add_subplot(2,3,i)

    ax.text(.5,.9,'centered title',
        horizontalalignment='center',
        transform=ax.transAxes)

    ax.plot(data)
show()

Plot showing text relative to axes bounding box.

Hope that helps!

Tobias
  • 4,034
  • 1
  • 28
  • 35
  • 1
    so if i supposedly wanted to add a b c d on the upper left edge of these plots, who were all different ranges, how would i do that? – tarrasch Oct 22 '12 at 12:29
  • @tarrasch In this case, you may want to use `(0.0,1.0)` for the coordinates (or something like `0.1,0.9` to allow for a bit of space), see [this answer](https://stackoverflow.com/a/8482667/9655481) and the [examples in the matplotlib docs](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.text.html) – Splines Aug 12 '22 at 10:20
40

Have you considered axes.set_title? You can also pass x and y coordinates as keyword arguments ax.set_title("my title", x=0.5, y=0.6).

Hope this helps.

Michael Hall
  • 2,834
  • 1
  • 22
  • 40
dmcdougall
  • 2,456
  • 1
  • 17
  • 15