How to Set a Single Main Title for All the Subplots in Matplotlib?
Prerequisites: Matplotlib
A title in matplotlib library describes the main subject of plotting the graphs. Setting a title for just one plot is easy using the title() method. By using this function only the individual title plots can be set but not a single title for all subplots. Hence, to set a single main title for all subplots, suptitle() method is used.
Syntax: suptitle(self, t, **kwargs)
Parameters: This method accept the following parameters that are discussed below:
- t : This parameter is the title text.
- x: This parameter is the x location of the text in figure coordinates.
- y: This parameter is the y location of the text in figure coordinates.
- horizontalalignment, ha : This parameter is the horizontal alignment of the text relative to (x, y).
- verticalalignment, va : This parameter is the vertical alignment of the text relative to (x, y).
- fontsize, size : This parameter is the font size of the text.
- fontweight, weight : This parameter is the font weight of the text.
Returns: This method returns the Text instance of the title.
Approach
- Import module
- Create data to be plotted
- Set figure title using suptitle()
- Plot the graph
- Display plot
Implementation using approach given is given below:
Example 1:
Python3
import matplotlib.pyplot as plt import numpy as np fig, (ax1, ax2) = plt.subplots( 1 , 2 , figsize = ( 12 , 5 )) x1 = [ 1 , 2 , 3 , 4 , 5 , 6 ] y1 = [ 45 , 34 , 30 , 45 , 50 , 38 ] y2 = [ 36 , 28 , 30 , 40 , 38 , 48 ] labels = [ "student 1" , "student 2" ] fig.suptitle( ' Student marks in different subjects ' , fontsize = 30 ) # Creating the sub-plots. l1 = ax1.plot(x1, y1, 'o-' , color = 'g' ) l2 = ax2.plot(x1, y2, 'o-' ) fig.legend([l1, l2], labels = labels, loc = "upper right" ) plt.subplots_adjust(right = 0.9 ) plt.show() |
Output:
Example 2:
Python3
import matplotlib.pyplot as plt import numpy as np fig, ax = plt.subplots( 2 , 2 ) fig.suptitle( '** Main Title for all sub plots **' , fontsize = 20 ) plt.style.use( 'seaborn' ) labels = [ 'first line' , 'second line' , 'third line' , 'fourth line' , 'fifth line' ] ax[ 0 , 0 ].plot([ 1 , 2 , 3 , 4 , 5 ], [ 9 , 3 , 5 , 7 , 9 ], '-.' , color = 'g' ) ax[ 0 , 0 ].set_title( 'first subplot' ) ax[ 0 , 0 ].set_yticks(np.arange( 1 , 10 )) ax[ 0 , 1 ].plot([ 1 , 2 , 3 , 4 , 5 , 6 ], [ 1 , 4 , 9 , 7 , 9 , 8 ], '-*' , color = 'm' ) ax[ 0 , 1 ].set_title( 'second subplot' ) ax[ 0 , 1 ].set_yticks(np.arange( 1 , 10 )) ax[ 1 , 0 ].plot([ 1 , 2 , 3 , 4 , 5 ], [ 8 , 5 , 2 , 3 , 3 ], '-v' , color = 'r' ) ax[ 1 , 0 ].set_title( 'third subplot' ) ax[ 1 , 0 ].set_yticks(np.arange( 1 , 9 )) ax[ 1 , 1 ].plot([ 1 , 2 , 3 , 4 , 5 , 6 ], [ 1 , 3 , 5 , 7 , 9 , 6 ], 'o-' , color = 'b' ) ax[ 1 , 1 ].plot([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 7 ], [ 7 , 8 , 6 , 5 , 2 , 2 , 4 , 6 ], 'o-' , color = 'g' ) ax[ 1 , 1 ].set_title( 'fourth subplot' ) ax[ 1 , 1 ].set_yticks(np.arange( 1 , 10 )) fig.tight_layout() fig.legend(ax, labels = labels, loc = "upper right" , borderaxespad = 0.1 ) fig.subplots_adjust(top = 0.85 ) plt.show() |
Output: