python画图|显式和隐式接口The explicit and the implicit interfaces

【1】引言

经历了一段时间的学习,发现一些基础的点还没有理解透,为此特意加强了一些学习,并获得了一些心得。今天就和大家一起分享一下显式和隐式接口的学习成果。

【2】官网教程

在学习进程中,数次使用到plt.plot()函数和ax.plot()函数来画图。

但历次学习的过程中,这两个函数并没有同时出现,在matplotlib官网教程里面似乎更喜欢ax.plot()函数。

不过无论教程何种风格,乖乖学习永远是第一要义,点击下述链接直达教程:

https://matplotlib.org/stable/users/explain/quick_start.html#quick-start

链接指向的页面内容非常丰富,请直接下拉到下图所示部分:

图1

在这里还有一个API指向链接,暂时略过。

【3】显示和隐式作图

【3.1】显示作图(the "object-oriented (OO) style")

官网教程对显示作图总结为目标导向OO型,且提供了代码,这里做一些注解工作:

首先是引入计算模块numpy和画图模块matplotlib:

import numpy as np #引入计算模块
import matplotlib.pyplot as plt #引入画图模块

然后定义自变量 :

x = np.linspace(0, 2, 100)  # Sample data.定义自变量

使用fig.ax定义画图:

fig, ax = plt.subplots(figsize=(5, 2.7), layout='constrained') #定义要画图,figsize定义图像的宽度和高度,layout可以自动调整标签图例不会和图形重叠

使用ax.plot()函数输出多个图形,在里面直接定义了不同的因变量计算式(x,x**2,x**3):

ax.plot(x, x, label='linear')  # Plot some data on the Axes.输出第一个图形,标签linear
ax.plot(x, x**2, label='quadratic')  # Plot more data on the Axes...输出第二个图形,标签quadratic
ax.plot(x, x**3, label='cubic')  # ... and some more.输出第三个图形,标签cubic

定义轴标签,图名,并要求输出图例:

ax.set_xlabel('x label')  # Add an x-label to the Axes.设置X轴名
ax.set_ylabel('y label')  # Add a y-label to the Axes.设置Y轴名
ax.set_title("Simple Plot")  # Add a title to the Axes.设置图名
ax.legend()  # Add a legend.自动输出图例

最后输出图形(该行代码教程缺失):

plt.show()#输出图形,教程中缺失该行

输出图像为:

图2

 至此的完整代码为:

import numpy as np #引入计算模块
import matplotlib.pyplot as plt #引入画图模块

x = np.linspace(0, 2, 100)  # Sample data.定义自变量

# Note that even in the OO-style, we use `.pyplot.figure` to create the Figure.
fig, ax = plt.subplots(figsize=(5, 2.7), layout='constrained') #定义要画图,figsize定义图像的宽度和高度,layout可以自动调整标签图例不会和图形重叠
ax.plot(x, x, label='linear')  # Plot some data on the Axes.输出第一个图形,标签linear
ax.plot(x, x**2, label='quadratic')  # Plot more data on the Axes...输出第二个图形,标签quadratic
ax.plot(x, x**3, label='cubic')  # ... and some more.输出第三个图形,标签cubic
ax.set_xlabel('x label')  # Add an x-label to the Axes.设置X轴名
ax.set_ylabel('y label')  # Add a y-label to the Axes.设置Y轴名
ax.set_title("Simple Plot")  # Add a title to the Axes.设置图名
ax.legend()  # Add a legend.自动输出图例
plt.show()#输出图形,教程中缺失该行

【3.2】隐示作图(the pyplot-style)

官网教程对隐式作图提供了代码,这里做一些注解工作。

但为了避免累赘,只需列出不同部分:

plt.figure(figsize=(5, 2.7), layout='constrained') #定义要画图,figsize定义图像的宽度和高度,layout可以自动调整标签图例不会和图形重叠
plt.plot(x, x, label='linear')  # Plot some data on the Axes.输出第一个图形,标签linear
plt.plot(x, x**2, label='quadratic')  # Plot more data on the Axes...输出第二个图形,标签quadratic
plt.plot(x, x**3, label='cubic')  # ... and some more.输出第三个图形,标签cubic
plt.xlabel('x label')  # Add an x-label to the Axes.设置X轴名
plt.ylabel('y label')  # Add a y-label to the Axes.设置Y轴名
plt.title("Simple Plot")  # Add a title to the Axes.设置图名

由上述代码可见,首先是fig.ax替换为plt.figure,然后是将ax.plot()函数全部更换为plt.plot()函数,最后在设置坐标轴名称这里将ax.set_xlabel()和ax.set_ylabel()更换为plt.xlabel()和plt.ylabel()。

运行代码后的输出图像与图2一模一样。

【3.3】官网推荐

官网教程对隐式作图进行了推荐:

Matplotlib's documentation and examples use both the OO and the pyplot styles. In general, we suggest using the OO style, particularly for complicated plots, and functions and scripts that are intended to be reused as part of a larger project. However, the pyplot style can be very convenient for quick interactive work.

【4】API分析

在图1中还有一个API链接,点击它,跳转:

Matplotlib Application Interfaces (APIs) — Matplotlib 3.9.2 documentation

这里对显式和隐式做了简洁的描述:

  • An explicit "Axes" interface that uses methods on a Figure or Axes object to create other Artists, and build a visualization step by step. This has also been called an "object-oriented" interface.

  • An implicit "pyplot" interface that keeps track of the last Figure and Axes created, and adds Artists to the object it thinks the user wants.

 意译过来就是:

显示:一步一步设置图形的各个属性,画图人自由掌握

隐式:优先直接套用旧模板,但也支持画图人增加属性设置

【5】拆分解析

前述特意突出了显示可以逐步设置属性,并且在官网解释中使用plt.subplot()函数进行了多个图像分解。为此,也尝试拆解一下:

【5.1】fig.ax显示拆解

将代码改写如下:

import numpy as np #引入计算模块
import matplotlib.pyplot as plt #引入画图模块

x = np.linspace(0, 2, 100)  # Sample data.定义自变量

# Note that even in the OO-style, we use `.pyplot.figure` to create the Figure.
fig, ax = plt.subplots(3,1,layout='constrained') #定义要画图,figsize定义图像的宽度和高度,layout可以自动调整标签图例不会和图形重叠
ax[0].plot(x, x, label='linear') # Plot some data on the Axes.输出第一个图形,标签linear
ax[0].set_xlabel('x label')  # Add an x-label to the Axes.设置X轴名
ax[0].set_ylabel('y label')  # Add a y-label to the Axes.设置Y轴名
ax[0].set_title("Simple Plot")  # Add a title to the Axes.设置图名
ax[0].legend()  # Add a legend.自动输出图例

ax[1].plot(x, x**2, label='quadratic')  # Plot more data on the Axes...输出第二个图形,标签quadratic
ax[1].set_xlabel('x label')  # Add an x-label to the Axes.设置X轴名
ax[1].set_ylabel('y label')  # Add a y-label to the Axes.设置Y轴名
ax[1].set_title("Simple Plot")  # Add a title to the Axes.设置图名
ax[1].legend()  # Add a legend.自动输出图例

ax[2].plot(x, x**3, label='cubic')  # ... and some more.输出第三个图形,标签cubic
ax[2].set_xlabel('x label')  # Add an x-label to the Axes.设置X轴名
ax[2].set_ylabel('y label')  # Add a y-label to the Axes.设置Y轴名
ax[2].set_title("Simple Plot")  # Add a title to the Axes.设置图名
ax[2].legend()  # Add a legend.自动输出图例

plt.show()#输出图形,教程中缺失该行

对fig.ax显示作图进行拆解,首先用plt.subplots(3,1,layout='constrained') 约定了三个3行1列的作图位置,然后每个ax都按照顺序输出,这样就完成了拆解画图:

图3

由图3可见,每个子图都采用同样的方式执行了输出:因为方式的定义完全一致。

【5.2】plt.figure隐式拆解

 输入以下代码:

import numpy as np #引入计算模块
import matplotlib.pyplot as plt #引入画图模块

x = np.linspace(0, 2, 100)  # Sample data.定义自变量

# Note that even in the OO-style, we use `.pyplot.figure` to create the Figure.
plt.figure(figsize=(5, 2.7), layout='constrained') #定义要画图,figsize定义图像的宽度和高度,layout可以自动调整标签图例不会和图形重叠
plt.subplot(3,1,1)
plt.plot(x, x, label='linear')  # Plot some data on the Axes.输出第一个图形,标签linear
plt.xlabel('x label')  # Add an x-label to the Axes.设置X轴名
plt.ylabel('y label')  # Add a y-label to the Axes.设置Y轴名
plt.title("Simple Plot-linear")  # Add a title to the Axes.设置图名
plt.legend()  # Add a legend.自动输出图例

plt.subplot(3,1,2)
plt.plot(x, x**2, label='quadratic')  # Plot more data on the Axes...输出第二个图形,标签quadratic
plt.xlabel('x label')  # Add an x-label to the Axes.设置X轴名
plt.ylabel('y label')  # Add a y-label to the Axes.设置Y轴名
plt.title("Simple Plot-quadratic")  # Add a title to the Axes.设置图名
plt.legend()  # Add a legend.自动输出图例

plt.subplot(3,1,3)
plt.plot(x, x**3, label='cubic')  # ... and some more.输出第三个图形,标签cubic
plt.xlabel('x label')  # Add an x-label to the Axes.设置X轴名
plt.ylabel('y label')  # Add a y-label to the Axes.设置Y轴名
plt.title("Simple Plot-cubic")  # Add a title to the Axes.设置图名
plt.legend()  # Add a legend.自动输出图例

plt.show()#输出图形,教程中缺失该行

对plt.figure显示作图进行拆解,分别用plt.subplot(3,1,x) 和plt.plot()约定了三个3行1列的作图位置,然后每个plot都按照顺序输出,这样就完成了拆解画图:

图4

由图4可见, 每个子图都采用同样的方式执行了输出:因为方式的定义完全一致。

然后很直观可见,图4没有图3好看。

追溯原因:图4对应代码设置了figuresize(5,2.7),为此尝试删除该部分代码,再次输出图形:

图5

由图5可见,图像放大、更加清晰。

其实对比代码还会发现:

  1. fig.ax用plt.subplots(3,1)函数进行定义,是一次性定义所有;
  2. plt.figure用plt.subplot(3,1,x)函数进行定义,是每一个子图都需要单独定义一次。

现实中,大家可以任选一种方式。

【6】总结

辨析了显式和隐式接口作图,即对比了plt.plot()函数和ax.plot()函数作图的区别,这两种方式都可以使用,且满足日常作图需要,大家可以灵活选用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值