Increase distance between title and plot in matplolib? [duplicate]
Asked Answered
J

5

104

I have a simple plot in matplotlib and I would like to increase the distance between the title and the plot (without using suptitle because it does not work on the version I use on a server). How to do that ?

Janeanjaneczka answered 7/5, 2013 at 12:58 Comment(2)
Isn't it possible to simply put some blank line(s) after the title? You could even play with the linespacing kwarg of text.Neighborhood
I found just adding \n after the string left the space I neededSelves
B
58

There doesn't seem to be a clean way to set this directly (but might be worth a feature request to add that), however the title is just a text artist, so you can reach in and change it.

#ax = plt.gca()
ttl = ax.title
ttl.set_position([.5, 1.05])
#plt.draw()

should do the trick. Tune the 1.05 to your liking.

Boyfriend answered 7/5, 2013 at 13:43 Comment(0)
N
158

With matplotlib 2.2+, you can use the keyword argument pad:

ax.set_title('Title', pad=20)

Adjust pad until you're happy with the axis title position. The advantage of this method over using rcParams is that it only changes this one axis title.

Nonappearance answered 20/3, 2018 at 21:44 Comment(2)
You can do ax.xaxis.labelpad=10.0 as well as yaxis and zaxisGuarantor
@TedoVrbanec For axis labels, you can do ax.set_xlabel('label', labelpad=20). Adjust the labelpad parameter until you're happy.Nonappearance
B
58

There doesn't seem to be a clean way to set this directly (but might be worth a feature request to add that), however the title is just a text artist, so you can reach in and change it.

#ax = plt.gca()
ttl = ax.title
ttl.set_position([.5, 1.05])
#plt.draw()

should do the trick. Tune the 1.05 to your liking.

Boyfriend answered 7/5, 2013 at 13:43 Comment(0)
C
47

You can just pass y parameter into plt.suptitle method:

plt.suptitle('Amazing Stats', size=16, y=1.12);      
Chingchinghai answered 5/11, 2018 at 15:8 Comment(1)
Good! In matplotlib v2.0.0 it works also for plt.title().Tupi
P
27

Using rcParams:

from matplotlib import rcParams
rcParams['axes.titlepad'] = 20 

where 20 is the padding between the plot and the title.

From https://matplotlib.org/users/customizing.html

Pretonic answered 11/6, 2017 at 15:5 Comment(4)
the documentation in the link only lists 'axes.titlepad'. possibly a typo?Limicoline
@Limicoline Correct. Thank you for spotting this. I have updated the answer.Pretonic
In Python3 "KeyError: 'axes.titlepad is not a valid rc parameter".Ferdinande
@GennaroTedesco are you using an up-to-date version of matplotlib?Pretonic
H
13

Another possibility is to reduce the relative size of the plot with respect to the whole figure window. In that way the distance between title and plot increases.

Before showing the plot, i.e. before plt.show(), write following command:

#The standard value of 'top' is 0.9,
#tune a lower value, e.g., 0.8
plt.subplots_adjust(top=0.8) 

This method has the advantage over @CanCeylan method that the title never goes out of the figure window; because if the title is large enough, then moving it upwards through the parameter y in suptitle might move the title outside the figure. (as it happened to me ;))

Hegumen answered 24/6, 2019 at 13:52 Comment(1)
i had an older version of matplotlib ... so i wasn't able to use the 'axes.titlepad' solutions. so yours worked great! thanks a million.Stank

© 2022 - 2024 — McMap. All rights reserved.