”;
What is Subplot Title?
Subplot titles in Matplotlib refer to the individual titles assigned to each subplot within a larger figure. When we have multiple subplots arranged in a grid such as a matrix of plots it”s often beneficial to add titles to each subplot to provide context or describe the content of that specific subplot.
Setting subplot titles is a useful practice when creating multiple visualizations within a single figure by enhancing the readability and comprehension of our overall plots. We have the method namely set_title() for setting the title of the subplot.
By utilizing set_title() we can add descriptive titles to individual subplots within a figure allowing for better organization and comprehension of complex visualizations.
Purpose of Subplot title
-
Provide Context − Subplot titles offer descriptive information about the content of each subplot within a larger figure aiding in better understanding the visualizations.
-
Differentiate Subplots − Titles help distinguish between multiple plots by allowing viewers to identify and interpret each subplot”s data or purpose easily.
Importance of Subplot title
-
Subplot titles help clarify the content or purpose of individual plots within a grid of subplots, especially when presenting multiple visualizations together.
-
They aid in quickly identifying the information presented in each subplot, improving the overall interpretability of the visualizations.
Syntax
The below is the syntax and parameters for setting up the subplot title.
ax.set_title(''Title'')
Where,
-
ax − It represents the axes object of the subplot for which the title is being set.
-
set_title() − The method used to set the title.
-
”Title” − It is the string representing the text of the title.
Subplots with title
In this example we are creating the subplots and setting up the title for each subplot by using the set_title() method available in the Matplotlib library.
Example
import matplotlib.pyplot as plt import numpy as np # Generating sample data x = np.linspace(0, 10, 100) y1 = np.sin(x) y2 = np.cos(x) # Creating subplots fig, (ax1, ax2) = plt.subplots(1, 2) # Plotting on the first subplot ax1.plot(x, y1) ax1.set_title(''Sine Wave'') # Plotting on the second subplot ax2.plot(x, y2) ax2.set_title(''Cosine Wave'') # Displaying the subplots plt.show()
Output
Example
Here in this example we are creating the subplots and adding the title to each subplot.
import matplotlib.pyplot as plt import numpy as np # Generating sample data x = np.linspace(0, 10, 50) y = np.sin(x) # Generating random data for scatter plot np.random.seed(0) x_scatter = np.random.rand(50) * 10 y_scatter = np.random.rand(50) * 2 - 1 # Random values between -1 and 1 # Creating subplots fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4)) # 1 row, 2 columns # Line plot on the first subplot ax1.plot(x, y, color=''blue'', label=''Line Plot'') ax1.set_title(''Line Plot'') ax1.set_xlabel(''X-axis'') ax1.set_ylabel(''Y-axis'') ax1.legend() # Scatter plot on the second subplot ax2.scatter(x_scatter, y_scatter, color=''red'', label=''Scatter Plot'') ax2.set_title(''Scatter Plot'') ax2.set_xlabel(''X-axis'') ax2.set_ylabel(''Y-axis'') ax2.legend() # Displaying the subplots plt.show()
Output
”;