当前位置: 首页 > news >正文

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()函数作图的区别,这两种方式都可以使用,且满足日常作图需要,大家可以灵活选用。


http://www.mrgr.cn/news/45349.html

相关文章:

  • can 总线入门———can简介硬件电路
  • Redis面试篇1
  • 也来猜猜 o1 实现方法
  • OpenCV高级图形用户界面(3)关闭由 OpenCV 创建的指定窗口函数destroyWindow()的使用
  • PCL-点云质心识别
  • 机器学习——强化学习与深度强化学习
  • JioNLP:一款实用的中文NLP预处理工具包
  • gligen安装部署笔记
  • pycharm连接linux服务器需要提前安装ssh服务
  • Collection 框架的结构
  • STM32的时钟复位控制单元(RCU/RCC)技术介绍
  • SpringBoot飘香水果网站:从概念到实现
  • 2024故障测试入门指南!
  • 基于单片机的烧水壶系统设计
  • 如何在VSCode上运行C/C++代码
  • 宠物咖啡馆数字化解决方案:基于SpringBoot的实现
  • 2024下《信息系统运行管理员》案例简答题,刷这些就够了!
  • Android 无Bug版 多语言设计方案!
  • redis 连接池
  • 深度学习模型