Matplotlib 简单教程 1:两种编码风格 + 图形结构
1、编码风格
python 的 matplotlib 库,提供了两种编码绘图风格(面向对象的编码,pyplot 函数编码),如下:
1.1、面向对象:OO-style
import numpy as np
import matplotlib.pyplot as plt
# 创建绘图数据
x = np.linspace(0, 2, 100)
# 创建一个绘图窗体,并设置大小为 (3.5, 3)
fig, ax = plt.subplots(figsize=(3.5, 3))
# 绘图
ax.plot(x, x, label='linear')
ax.plot(x, x**2, label='quadratic')
# 设置标题
ax.set_title("Simple Plot")
# 设置轴标签
ax.set_xlabel('x label')
ax.set_ylabel('y label')
# 设置图例
ax.legend()
# 保存图形为 PNG 格式
plt.savefig('matplotlib_01.png'
, dpi=300
, bbox_inches='tight')
# 显示图形
plt.show()
1.2、pyplot 函数
import numpy as np
import matplotlib.pyplot as plt
# 创建绘图数据
x = np.linspace(0, 2, 100)
# 创建一个绘图窗体,并设置大小为 (3.5, 3)
plt.figure(figsize=(3.5, 3))
# 绘图
plt.plot(x, x, label='linear')
plt.plot(x, x**2, label='quadratic')
# 设置标题
plt.title("Simple Plot")
# 设置轴标签
plt.xlabel('x label')
plt.ylabel('y label')
# 设置图例
plt.legend()
# 显示图形
plt.show()
2、图形结构
2.1、Figure
- Figure 是整个窗体,你可以把它想象成一个画板,我们在其上面创建图形进,这是最为外层的对象。
- Figure 可以将其看作是整个绘图区域的容器
- 一个 Figure 对象可以包含一或多个 Axes (子图)对象。
面向对象的编码
import matplotlib.pyplot as plt
# an empty figure with no Axes
fig = plt.figure(figsize=(3.5, 3))
plt.show()
输出:<Figure size 350x300 with 0 Axes>
pyplot 函数编码
import matplotlib.pyplot as plt
# an empty figure with no Axes
plt.figure(figsize=(3.5, 3))
plt.show()
输出:<Figure size 350x300 with 0 Axes>
2.2、Axes
- Axes (子图)对象,是实际进行绘图操作的区域,包含坐标轴、数据点、线条、图例等元素。
- Axes 可以将其看作是包含绘图元素(例如线、点、刻度、标签、图例等)的一个容器。
- Axes 指子图,每个子图可以用任一种坐标系表示。如笛卡尔坐标系(直角坐标系),它包含两个 Axis 对象。每个 Axes 都有一个标题,一个 x 标签和一个 y 标签。在这个坐标系内,我们可以绘制各种图形。
面向对象的编码
import matplotlib.pyplot as plt
# Create a figure with a single Axes
fig, ax = plt.subplots(figsize=(3.5, 3))
plt.show()
pyplot 函数编码
import matplotlib.pyplot as plt
# Create a figure
plt.figure(figsize=(3.5, 3))
# Create a single Axes
plt.plot()
plt.show()
2.3、Axis
- Axis (轴)对象,用于控制图形中的刻度、刻度标签和网格线。
- 每个 Axes 对象有一个 XAxis 对象和一个 YAxis 对象。
- Axis 的主要功能包括:设置标签格式;刻度位置;刻度标签、字体、颜色;刻度线粗细、颜色;网格线显示与否、样式等;
面向对象的编码
import matplotlib.pyplot as plt
# Create a figure containing a single Axes.
fig, ax = plt.subplots(figsize=(3.5, 3))
# Plot some data on the Axes.
ax.plot([1, 2, 3, 4], [1, 4, 2, 3])
# Create label formatting dictionary
label_format = {
'fontproperties': 'Georgia'
,'fontsize': 10
,'color': 'Gray'
,'fontweight': 'bold'
}
# Set title
ax.set_title('This is Title', **label_format, size=15)
# Set labels for the x-axis and y-axis
ax.set_xlabel('X Axis Title Here', **label_format)
ax.set_ylabel('Y Axis Title Here', **label_format)
# Show the figure
plt.show()
pyplot 函数编码
import matplotlib.pyplot as plt
plt.figure(figsize=(3.5, 3))
plt.plot([1, 2, 3, 4], [1, 4, 2, 3])
# Create label formatting dictionary
label_format = {
'fontproperties': 'Georgia'
,'fontsize': 10
,'color': 'Gray'
,'fontweight': 'bold'
}
# Set title
plt.title('This is Title', **label_format, size=15)
# Set labels for the x-axis and y-axis
plt.xlabel('X Axis Title Here', **label_format)
plt.ylabel('Y Axis Title Here', **label_format)
# Show the figure
plt.show()
总结:以上介绍了 Matplotlib 的图形结构,包括 Figure、Axes 和 Axis 对象。以及如何使用面向对象和 pyplot 函数创建图形。