Matplotlib 简明教程
Matplotlib - Subplot Titles
What is Subplot Title?
在 Matplotlib 中的 Subplot titles 指的是分配给更大的图像中每个子图的各个标题。当我们有多个以网格排列的子图(例如,绘图矩阵)时,通常最好为每个子图添加标题以提供上下文或描述该特定子图的内容。
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.
创建多个可视化效果时,设置子图标题是一种有用的做法,因其可以增强我们整体绘图的可读性和理解性。我们有方法 set_title() 用于设置子图的标题。
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.
通过使用 set_title() ,我们可以向图中的各个子图添加描述性标题,从而更好地组织和理解复杂的可视化效果。
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
在此示例中,我们使用 Matplotlib 库中提供的 set_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.
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()
在此示例中,我们创建子图并向每个子图添加标题。
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()