Matplotlib 简明教程
Matplotlib - Spines
What are Spines?
在 Matplotlib 库中, spines 指的是构成数据区域框架的绘图边框或边。这些脊线包含绘图的边界,定义了显示数据点区域。默认情况下,一个绘图有四个脊线,例如顶部、底部、左侧和右侧。
In Matplotlib library spines refer to the borders or edges of a plot that frame the data area. These spines encompass the boundaries of the plot defining the area where data points are displayed. By default a plot has four spines such as top, bottom, left and right.
通过允许定制外观、颜色、厚度和可见度,在 Matplotlib 中操纵脊线提供了设计绘图视觉效果的灵活性,从而更量身定制并美观地展示数据。
Manipulating spines in Matplotlib offers flexibility in designing the visual aspects of a plot by allowing for a more tailored and aesthetically pleasing presentation of data.
Key Characteristics of Spines
以下是脊线的特征。
The following are the characteristics of spines.
Borders of the Plot - 脊线形成绘图区域的边框,包裹可视化数据的区域。
Borders of the Plot − Spines form the borders of the plot area enclose the region where data is visualized.
Configurable Properties - 可以通过允许调整外观、颜色、厚度和可见度来分别自定义每个脊线(顶部、底部、左侧和右侧)。
Configurable Properties − Each spine top, bottom, left, and right can be customized individually by allowing adjustments to their appearance, color, thickness and visibility.
Visibility Control - 可以显示或隐藏脊线以修改绘图的外观。
Visibility Control − Spines can be made visible or hidden to modify the appearance of the plot.
Uses of Spines
Plot Customization - 脊线允许自定义绘图的外观,从而可以调整绘图的边界和样式。
Plot Customization − Spines allow customization of the plot’s appearance enabling adjustments to the plot’s boundaries and style.
Aesthetics and Visualization - 自定义脊线可以增强绘图的美观性,并将注意力吸引到特定关注区域。
Aesthetics and Visualization − Customizing spines can enhance the aesthetics of the plot and draw attention to specific areas of interest.
Types of spines
现在,让我们详细了解绘图中可用的每个脊线。
Now let’s see the each and every spine available in a plot in detail.
Top Spine
顶部脊线是指绘图区域顶部水平线,它对应于 y 轴的上边界。它是构成绘图周围边框的四个脊线之一,如顶部、底部、左侧和右侧。
The Top spine refers to the horizontal line at the top of the plot area that corresponds to the upper boundary of the y-axis. It’s one of the four spines such as top, bottom, left and right that form the borders around the plot.
Boundary Line - 顶部虚线表示沿 y 轴的绘图区域的上边界。
Boundary Line − The top spine represents the upper boundary of the plot area along the y-axis.
Default Visibility - 默认情况下,顶部虚线在 Matplotlib 绘图中可见。
Default Visibility − By default the top spine is visible in Matplotlib plots.
Customization - 与其他虚线类似,顶部虚线可以根据其可见性、颜色、线型和线宽进行自定义。
Customization − Similar to other spines the top spine can be customized in terms of its visibility, color, linestyle and linewidth.
在这个示例中, ax.spines['top'].set_visible(False) 通过移除沿 y 轴的绘图区域的上边界来隐藏顶部虚线。
In this example ax.spines['top'].set_visible(False) hides the top spine by removing the upper boundary of the plot area along the y-axis.
import matplotlib.pyplot as plt
# Creating a simple plot
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y)
# Accessing and modifying the top spine
ax = plt.gca() # Get the current axes
ax.spines['top'].set_visible(False) # Hide the top spine
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Plot with Hidden Top Spine')
plt.show()
Aesthetic Control - 自定义顶部虚线的可见性、颜色或样式可以提升外观或匹配特定的设计要求。
Aesthetic Control − Customizing the visibility, color or style of the top spine can improve the appearance or match specific design requirements.
Adjusting Plot Boundaries - 当绘图不需要上边界或创建特定的视觉效果时,隐藏顶部虚线可能非常有用。
Adjusting Plot Boundaries − Hiding the top spine might be useful when the plot doesn’t require an upper boundary or when creating specific visual effects.
Bottom Spine
在 Matplotlib 中,底部虚线是指形成绘图区域与 x 轴对应的下边界的水平线。
In Matplotlib the bottom spine refers to the horizontal line that forms the bottom border of the plot area corresponding to the x-axis.
Association with x-axis - 底部虚线表示沿 x 轴的绘图边界,定义绘图区域的下边界。
Association with x-axis − The bottom spine represents the border of the plot along the x-axis defining the lower boundary of the plot area.
Customization - 与其他虚线类似,底部虚线可以根据其可见性、颜色、线型、厚度和位置进行自定义。
Customization − Similar to other spines the bottom spine can be customized in terms of its visibility, color, line style, thickness and position.
在这个示例中, ax.spines['bottom'].set_color('blue') 通过使用将底部虚线颜色更改为蓝色, ax.spines['bottom'].set_linewidth(2) 设置底部虚线厚度为 2,并且 ax.spines['bottom'].set_visible(True) 确保底部虚线在隐藏的情况下可见。
In this example by using ax.spines['bottom'].set_color('blue') changes the color of the bottom spine to blue, ax.spines['bottom'].set_linewidth(2) sets the thickness of the bottom spine to 2 and ax.spines['bottom'].set_visible(True) ensures the bottom spine is visible if in case it was hidden.
import matplotlib.pyplot as plt
# Creating a simple plot
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y)
# Accessing and customizing the bottom spine
ax = plt.gca() # Get the current axes
ax.spines['bottom'].set_color('blue') # Change the color of the bottom spine to blue
ax.spines['bottom'].set_linewidth(2) # Set the thickness of the bottom spine to 2
ax.spines['bottom'].set_visible(True) # Make the bottom spine visible (if previously hidden)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Plot with Customized Bottom Spine')
plt.show()
Emphasizing Axes - 通过自定义底部虚线可以吸引对 x 轴的注意并增强绘图的美观性。
Emphasizing Axes − By customizing the bottom spine can draw attention to the x-axis and enhance the plot’s aesthetics.
Highlighting Plot Boundaries - 通过调整底部虚线的外观,可以帮助描绘绘图区域并提高其清晰度。
Highlighting Plot Boundaries − By adjusting the appearance of the bottom spine can help in delineating the plot area and improving its clarity.
Left Spine
在 Matplotlib 中,左侧虚线是指形成绘图区域左侧边界的垂直线,对应于 y 轴。
In Matplotlib the left spine refers to the vertical line that forms the left border of the plot area, corresponding to the y-axis.
Association with y-axis - 左侧虚线表示沿着 y 轴的绘图边界,定义绘图区域的左边界。
Association with y-axis − The left spine represents the border of the plot along the y-axis defining the left boundary of the plot area.
Customization - 左侧虚线的自定义类似于其他松树,可以通过颜色、可见性、边框宽度等进行自定义。
Customization − The customization of the left spine is similar to the other pines which can be customized by color, visibility, border width etc.
在这个示例中, ax.spines['left'].set_color('green') 更改左侧虚线颜色为绿色, ax.spines['left'].set_linewidth(2) 设置左侧虚线厚度为 2,并且 ax.spines['left'].set_visible(False) 确保左侧虚线在可见的情况下不可见。
In this example ax.spines['left'].set_color('green') changes the color of the left spine to green, ax.spines['left'].set_linewidth(2) sets the thickness of the left spine to 2 and ax.spines['left'].set_visible(False) ensures the left spine is invisible if in case it was visible.
import matplotlib.pyplot as plt
# Creating a simple plot
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y)
# Accessing and customizing the left spine
ax = plt.gca() # Get the current axes
ax.spines['left'].set_color('green') # Change the color of the left spine to green
ax.spines['left'].set_linewidth(2) # Set the thickness of the left spine to 2
ax.spines['left'].set_visible(False) # Make the left spine invisible (if previously visible)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Plot with Customized Left Spine')
plt.show()
Right Spine
在 Matplotlib 中,右侧虚线表示形成绘图区域右侧边界的垂直线,对应于右侧的 y 轴。
In Matplotlib the right spine represents the vertical line forming the right border of the plot area corresponding to the y-axis on the right side.
Associated with y-axis - 右侧虚线定义绘图在沿 y 轴的右侧边界,表示绘图右侧的 y 轴。
Associated with y-axis − The right spine defines the right border of the plot along the y-axis representing, the y-axis on the right side of the plot.
Customization - 与其他虚线类似,右侧虚线可以根据其可见性、颜色、线型、厚度和位置进行自定义。
Customization − Similar to other spines the right spine can be customized in terms of its visibility, color, line style, thickness and position.
在此示例中,我们使用 ax.spines['right'] 自定义图表的右侧边框。
In this example we are using ax.spines['right'] to customize the right spine to the plot.
import matplotlib.pyplot as plt
# Creating a simple plot
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y)
# Accessing and customizing the right spine
ax = plt.gca() # Get the current axes
ax.spines['right'].set_color('green') # Change the color of the right spine to green
ax.spines['right'].set_linewidth(2) # Set the thickness of the right spine to 2
ax.spines['right'].set_visible(True) # Make the right spine visible (if previously hidden)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Plot with Customized Right Spine')
plt.show()
Customize spines of Matplotlib figures
在此示例中,我们将创建六个图形以查看并自定义其边框。
In this example we are creating six figures to see and customize the spines for it.
#First import the required libraries for the workbook.
import numpy as np
import matplotlib.pyplot as plt
#draw graph for sines
theta = np.linspace(0, 2*np.pi, 128)
y = np.sin(theta)
fig = plt.figure(figsize=(8,6))
#Define the axes with default spines
ax1 = fig.add_subplot(2, 3, 1)
ax1.plot(theta, np.sin(theta), 'b-*')
ax1.set_title('default spines')
#Define the function to plot the graph
def plot_graph(axs, title, lposition, bposition):
ax = fig.add_subplot(axs)
ax.plot(theta, y, 'b-*')
ax.set_title(title)
ax.spines['left'].set_position(lposition)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_position(bposition)
ax.spines['top'].set_visible(False)
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
#plot 3 graphs
plot_graph(232, 'centered spines', 'center', 'center')
plot_graph(233, 'zeroed spines', 'zero', 'zero')
plot_graph(234, 'spines at axes [0.25, 0.75]', ('axes', 0.25),('axes', 0.75))
plot_graph(235, 'spines at data [1.0, -1.0]', ('data', 1.0),('data', -1.0))
plot_graph(236, 'adjusted spines', ('outward', 10), ('outward', 10))
#fit the plot in the grid and display.
plt.tight_layout()
plt.show()