Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
187 views
in Technique[技术] by (71.8m points)

python - Title of figure between the subplots

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?

question from:https://stackoverflow.com/questions/65599658/combined-pie-chart-and-bar-plot-python-matplotlib

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

enter image description here

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

enter image description here

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...