理解matplotlib中的subplot功能
最编程
2024-02-20 14:23:06
...
前言
在看Matplotlib时,对subplots在一个图上画多个ax有一些不明白的地方,通过画图才得以理解,因此记录一下。
正文
在 Matplotlib Simple Plot 的代码中,有这样一行:
fig, ax = plt.subplots()
plt.subplots()
返回一个 Figure实例fig 和一个 AxesSubplot实例ax 。这个很好理解,fig代表整个图像,ax代表坐标轴和画的图。
但是,将上述代码改为
fig, ax = plt.subplots(2,3)
时fig依旧是Figure实例,ax就变成ndarray了,显示一下fig:
其实ax很好理解:
我们看一下ax的元素是什么:
print (ax)
#输出:
[[<matplotlib.axes._subplots.AxesSubplot object at 0x000000CED4AEB5C0>
<matplotlib.axes._subplots.AxesSubplot object at 0x000000CECD469630>
<matplotlib.axes._subplots.AxesSubplot object at 0x000000CECD27C898>]
[<matplotlib.axes._subplots.AxesSubplot object at 0x000000CED4779CF8>
<matplotlib.axes._subplots.AxesSubplot object at 0x000000CECD4477B8>
<matplotlib.axes._subplots.AxesSubplot object at 0x000000CECD1BC748>]]
说明ax是保存 AxesSubplot实例 的 ndarray数组。
看一下ax中的元素:
print(ax[0][0])
print(ax[0][1])
print(ax[0][2])
print(ax[1][0])
print(ax[1][1])
print(ax[1][2])
#输出:
Axes(0.125,0.536818;0.227941x0.343182)
Axes(0.398529,0.536818;0.227941x0.343182)
Axes(0.672059,0.536818;0.227941x0.343182)
Axes(0.125,0.125;0.227941x0.343182)
Axes(0.398529,0.125;0.227941x0.343182)
Axes(0.672059,0.125;0.227941x0.343182)
我们在将上述Axes画在一个图中:
import matplotlib.pyplot as plt
import matplotlib.patches as patches
fig1 = plt.figure()
ax1 = fig1.add_subplot(111, aspect='equal')
ax1.add_patch(
patches.Rectangle(
(0.125,0.536818), # (x,y)
0.227941, # width
0.343182, # height
)
)
ax1.add_patch(
patches.Rectangle(
(0.398529,0.536818), # (x,y)
0.227941, # width
0.343182, # height
)
)
ax1.add_patch(
patches.Rectangle(
(0.672059,0.536818), # (x,y)
0.227941, # width
0.343182, # height
)
)
ax1.add_patch(
patches.Rectangle(
(0.125,0.125), # (x,y)
0.227941, # width
0.343182, # height
)
)
ax1.add_patch(
patches.Rectangle(
(0.398529,0.125), # (x,y)
0.227941, # width
0.343182, # height
)
)
ax1.add_patch(
patches.Rectangle(
(0.672059,0.125), # (x,y)
0.227941, # width
0.343182, # height
)
)
# fig1.savefig('rect1.png', dpi=90, bbox_inches='tight')
plt.show()
输出:
这样就可以诠释plt.subplots
的返回值了。
完。
推荐阅读
-
Matplotlib 中几种默认颜色的十六进制表示和预览
-
缩写 hse 的含义是什么_如何理解邮件中的 "CC、PS、FYI "和其他英文缩写?
-
73.Vivado 的误用和改进 - 在 Vivado 中实施 ECO 功能
-
了解子网掩码的功能及其在网络规划中的重要性
-
微信 "扫一扫 "物联网,全面揭秘 "扫一扫 "背后的扫盲技术!-1.1 扫一扫感知物体是做什么的? 1.1 微信扫一扫是做什么的? 扫一扫识物是指以图片或视频(商品图片:鞋/包/美妆/服饰/家电/玩具/图书/食品/珠宝/家具/其他商品)为输入媒介,挖掘微信内容生态中的有价值信息(电商+百科+资讯,如图1所示),并展示给用户。这里的电商基本涵盖了微信小程序覆盖上亿SKU的全量优质电商,可以支持用户货比N家并直接下单购买,百科和资讯则聚合了微信内的头部自媒体如搜狗、搜搜、百度等,向用户展示和分享拍摄商品相关的内容资讯。 图 1 扫一扫识别功能示意图 欢迎大家更新iOS新版微信→扫一扫→识货,亲自体验,也欢迎大家通过识货界面的反馈按钮向我们提交反馈意见。 扫一扫识物实景图展示 1.2 扫一扫识物有哪些使用场景? 扫一扫识物的目的是为用户访问微信内部生态内容开辟一个新窗口,以用户扫图片为输入形式,为用户提供微信生态内容中的百科、资讯、电商等作为展示页面。除了用户熟悉的扫一扫操作外,我们还将进一步拓展长按操作,让用户更方便地进行扫一扫操作。"扫一扫知事 "的落地场景主要涵盖三大部分: a. 科普知识: a.科普知识。用户通过扫一扫,可以在微信生态圈中获取该对象的百科、资讯等常识或趣闻,帮助用户更好地了解该对象; b.购物场景。同样的搜索功能支持用户看到喜欢的商品立即检索到微信小程序电商中的同款商品,支持用户即扫即购; c.广告场景。扫一扫识别物体可以辅助公众号文章、视频更好地理解其中蕴含的图片信息,从而更好地投放匹配广告,提高点击率。 1.3 Sweep Sense 为 Sweep 家族带来了哪些新技术? 对于扫一扫来说,大家耳熟能详的应该就是扫一扫二维码、扫一扫小程序码、扫一扫条形码、扫一扫翻译了。无论是各种形式的编码还是文字字符,都可以看作是图片的一种特定编码形式,而物的识别则是对自然场景图片的识别,这对于扫一扫家族来说是一个质的飞跃,我们希望从物的识别入手,进一步拓展扫一扫对自然场景图片的理解能力,比如扫酒、扫车、扫植物、扫人脸等服务,如下图3所示。 图 3 Sweep 家族
-
用最简单的 js 调用 VUE 中的微信扫一扫功能
-
Java面试 "带你一次性了解面试中的必要问题,谈谈你对ES的理解。
-
ES6、ES7、ES8、ES9 和 ES10 中的新功能列表
-
深入了解 Open CASCADE 中的 SelectMgr_EntityOwner 和高亮功能
-
一种结构设计模式,允许在对象中动态添加新行为。它通过创建一个封装器来实现这一目的,即把对象放入一个装饰器类中,然后把这个装饰器类放入另一个装饰器类中,以此类推,形成一个封装器链。这样,我们就可以在不改变原始对象的情况下动态添加新行为或修改原始行为。 在 Java 中,实现装饰器设计模式的步骤如下: 定义一个接口或抽象类作为被装饰对象的基类。 公共接口 Component { void operation; } } 在本例中,我们定义了一个名为 Component 的接口,该接口包含一个名为 operation 的抽象方法,该方法定义了被装饰对象的基本行为。 定义一个实现基类方法的具体装饰对象。 公共类 ConcreteComponent 实现 Component { public class ConcreteComponent implements Component { @Override public void operation { System.out.println("ConcreteComponent is doing something...") ; } } 定义一个抽象装饰器类,该类继承于基类,并将装饰对象作为一个属性。 公共抽象类装饰器实现组件 { protected Component 组件 public Decorator(Component component) { this.component = component; } } @Override public void operation { component.operation; } } } 在这个示例中,我们定义了一个名为 Decorator 的抽象类,它继承了 Component 接口,并将被装饰对象作为一个属性。在操作方法中,我们调用了被装饰对象上的同名方法。 定义一个具体的装饰器类,继承自抽象装饰器类并实现增强逻辑。 公共类 ConcreteDecoratorA extends Decorator { public ConcreteDecoratorA(Component 组件) { super(component); } } public void operation { super.operation System.out.println("ConcreteDecoratorA 正在添加新行为......") ; } } 在本例中,我们定义了一个名为 ConcreteDecoratorA 的具体装饰器类,它继承自装饰器抽象类,并实现了操作方法的增强逻辑。在操作方法中,我们首先调用被装饰对象上的同名方法,然后添加新行为。 使用装饰器增强被装饰对象。 公共类 Main { public static void main(String args) { Component 组件 = new ConcreteComponent; component = new ConcreteDecoratorA(component); 组件操作 } } 在这个示例中,我们首先创建了一个被装饰对象 ConcreteComponent,然后通过 ConcreteDecoratorA 类创建了一个装饰器,并将被装饰对象作为参数传递。最后,调用装饰器的操作方法,实现对被装饰对象的增强。 使用场景 在 Java 中,装饰器模式被广泛使用,尤其是在 I/O 中。Java 中的 I/O 库使用装饰器模式实现了不同数据流之间的转换和增强。 让我们打开文件 a.txt,从中读取数据。InputStream 是一个抽象类,FileInputStream 是专门用于读取文件流的子类。BufferedInputStream 是一个支持缓存的数据读取类,可以提高数据读取的效率,具体代码如下: @Test public void testIO throws Exception { InputStream inputStream = new FileInputStream("C:/bbb/a.txt"); // 实现包装 inputStream = new BufferedInputStream(inputStream); byte bytes = new byte[1024]; int len; while((len = inputStream.read(bytes)) != -1){ System.out.println(new String(bytes, 0, len)); } } } } 其中 BufferedInputStream 对读取数据进行了增强。 这样看来,装饰器设计模式和代理模式似乎有点相似,接下来让我们讨论一下它们之间的区别。 第三,与代理模式的区别: 代理模式的目的是控制对对象的访问,它在对象外部提供一个代理对象来控制对原对象的访问。代理对象和原始对象通常实现相同的接口或继承相同的类,以确保两者可以相互替换。 装饰器模式的目的是动态增强对象的功能,而这是通过对象内部的包装器来实现的。在装饰器模式中,装饰器类和被装饰对象通常实现相同的接口或继承自相同的类,以确保两者可以相互替代。装饰器模式也被称为封装器模式。 在代理模式中,代理类附加了与原类无关的功能。