Python 在matplolib中增加标题和情节之间的距离?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16419670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:34:09  来源:igfitidea点击:

Increase distance between title and plot in matplolib?

pythonmatplotlibplottitlemargins

提问by Vincent

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

我在 matplotlib 中有一个简单的图,我想增加标题和图之间的距离(不使用,suptitle因为它不适用于我在服务器上使用的版本)。怎么做 ?

采纳答案by 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 textartist, so you can reach in and change it.

似乎没有一种直接设置它的干净方法(但可能值得一个功能请求添加它),但是标题只是一个text艺术家,因此您可以访问并更改它。

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

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

应该做的伎俩。1.05根据自己的喜好调整。

回答by bmorgan

Using rcParams:

使用rcParams

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

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

20情节和标题之间的填充在哪里。

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

来自https://matplotlib.org/users/customizing.html

回答by Thucydides411

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

使用matplotlib 2.2+,您可以使用关键字参数pad

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

Adjust paduntil you're happy with the axis title position. The advantage of this method over using rcParamsis that it only changes this one axis title.

调整pad直到您对轴标题位置感到满意为止。这种方法比使用的优点rcParams是它只改变这个轴标题。

回答by CanCeylan

You can just pass yparameter into plt.suptitlemethod:

您可以将y参数传递给plt.suptitle方法:

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

回答by loved.by.Jesus

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:

在显示绘图之前plt.show(),即 before ,编写以下命令:

#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 yin suptitlemight move the title outside the figure. (as it happened to me ;))

与@CanCeylan 方法相比,此方法的优势在于标题永远不会超出图形窗口;因为如果标题是足够大的,然后向上移动它通过参数ysuptitle可以移动的称号附图之外。(因为它发生在我身上;))