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

纵然是瞬间 提交于 2019-11-27 21:05:50
tacaswell

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.

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.

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

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

plt.suptitle('Amazing Stats', size=16, y=1.12);      

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 ;))

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!