Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib placement of text e.g. suptitle inside the frame

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.

like image 900
tarrasch Avatar asked Oct 18 '12 15:10

tarrasch


People also ask

How do I change the position of a title in Matplotlib?

Matplotlib can display plot titles centered, flush with the left side of a set of axes, and flush with the right side of a set of axes. Automatic positioning can be turned off by manually specifying the y keyword argument for the title or setting rcParams["axes. titley"] (default: None ) in the rcParams.

How do you set the location of a title in Python?

In this method, we will be using the pad argument of the title() function to change the title location in the given plot in the python programming language. Example: In this example, We will elevateTitleTitle by using the “pad” parameter. The offset of the Title from the top of the axes, in points.


1 Answers

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!

like image 161
Tobias Avatar answered Sep 30 '22 20:09

Tobias