Title of figure between the subplots

前端 未结 1 1221
滥情空心
滥情空心 2021-01-28 09:30

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={\'heig         


        
1条回答
  •  日久生厌
    2021-01-28 10:16

    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.

    0 讨论(0)
提交回复
热议问题