Title of figure between the subplots

妖精的绣舞 提交于 2021-02-05 11:48:53

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/53781846/title-of-figure-between-the-subplots

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