子图之间的图形标题 [英] Title of figure between the subplots

查看:48
本文介绍了子图之间的图形标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我通过以下方式制作带有两个子图的图形时:

 将matplotlib.pyplot导入为pltfig=plt.figure(1)(ax1,ax2) = fig.subplots(2,1, gridspec_kw={'height_ratios':[1,15]})

标题出现在子图之间:

  plt.title('Title')plt.show()

我该如何在图的顶部显示标题?

解决方案

您正在寻找的是

使用plt.suptitle

 将matplotlib.pyplot导入为pltfig=plt.figure(1)(ax1,ax2)=图子图(2,1,gridspec_kw = {'height_ratios':[1,15]})plt.suptitle('标题')

根据 @ImportanceOfBeingErnest 的建议,您也可以使用 ax1.set_title('Title')将标题放在顶部,因为 ax1 对应于您案例中的顶部子图形.

When I make a figure with two subplots in the following way:

import matplotlib.pyplot as plt
fig=plt.figure(1)
(ax1,ax2) = fig.subplots(2,1, gridspec_kw={'height_ratios':[1,15]})

the title appears between the subplots:

plt.title('Title')
plt.show()

How can I have the title on the top of the figure instead?

解决方案

What you are looking for is suptitle which places a centered title at the top of the figure.

Using plt.title (applies to the current axis which is ax2 in your case)

import matplotlib.pyplot as plt
fig=plt.figure(1)
(ax1,ax2) = fig.subplots(2,1, gridspec_kw={'height_ratios':[1,15]})

plt.title('Title')

Using plt.suptitle

import matplotlib.pyplot as plt
fig=plt.figure(1)
(ax1,ax2) = fig.subplots(2,1, gridspec_kw={'height_ratios':[1,15]})

plt.suptitle('Title')

As suggested by @ImportanceOfBeingErnest , you can also use ax1.set_title('Title') to put the title on the top because ax1 corresponds to the top sub figure in your case.

这篇关于子图之间的图形标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆