Matplotlib实现subplot和subplots简单对比

作者:如是雨林 时间:2022-07-29 05:49:10 

前言:

大家一般都知道subplot可以画子图,但是subplots也可以画子图,鉴于subplots介绍比较少,这里做一个对比,两者没有功能一致。

对比开始:

需求:画出两张子图,在一行显示,子图中的内容一模一样

subplot代码:


ax1 = plt.subplot(1,2,1)
ax1.scatter(positive['X1'], positive['X2'], s=50, marker='x', label='Positive')
ax1.scatter(negative['X1'], negative['X2'], s=50, marker='o', label='Negative')
ax1.legend()#添加图列就是右上角的点说明
ax2 = plt.subplot(1,2,2)
ax2.scatter(positive['X1'], positive['X2'], s=50, marker='x', label='Positive')
ax2.scatter(negative['X1'], negative['X2'], s=50, marker='o', label='Negative')
ax2.legend()#添加图列就是右上角的点说明

Matplotlib实现subplot和subplots简单对比 

subplots代码


fig, ax = plt.subplots(figsize=(12,8),ncols=2,nrows=1)#该方法会返回画图对象和坐标对象ax,figsize是设置子图长宽(1200,800)
ax[0].scatter(positive['X1'], positive['X2'], s=50, marker='x', label='Positive')
ax[0].scatter(negative['X1'], negative['X2'], s=50, marker='o', label='Negative')
ax[0].legend()#添加图列就是右上角的点说明
ax[1].scatter(positive['X1'], positive['X2'], s=50, marker='x', label='Positive')
ax[1].scatter(negative['X1'], negative['X2'], s=50, marker='o', label='Negative')
ax[1].legend()#添加图列就是右上角的点说明

Matplotlib实现subplot和subplots简单对比

对比结果:

可以看出来两者都可以实现画子图功能,只不过subplots帮我们把画板规划好了,返回一个坐标数组对象,而subplot每次只能返回一个坐标对象,subplots可以直接指定画板的大小。

参考博客:Matplotlib的子图subplot的使用

参考博客:subplots与figure函数参数解释说明以及简单的使用脚本实例

来源:https://blog.csdn.net/qq_23418043/article/details/81813903

标签:Matplotlib,subplot,subplots
0
投稿

猜你喜欢

  • PHP中MVC模式的模板引擎开发经验分享

    2023-11-18 14:28:08
  • 在pycharm中文件取消用 pytest模式打开的操作

    2022-06-20 18:16:19
  • k8s容器互联flannel vxlan通信原理

    2024-05-08 10:46:01
  • Python pygorithm模块用法示例【常见算法测试】

    2023-05-19 00:23:59
  • 微信小程序如何调用图片接口API并居中显示

    2023-08-09 15:05:30
  • Numpy之reshape()使用详解

    2022-11-12 22:03:02
  • Python中定时任务框架APScheduler的快速入门指南

    2021-07-16 02:51:21
  • PHP之数组学习

    2024-05-02 17:35:43
  • JS与jQ读取xml文件的方法

    2024-04-19 10:13:22
  • python的图形用户界面介绍

    2023-04-08 19:55:46
  • MySQL字符集乱码及解决方案分享

    2024-01-21 12:00:43
  • SQLSERVER 中datetime 和 smalldatetime类型分析说明

    2024-01-23 23:36:13
  • python 解决mysql where in 对列表(list,,array)问题

    2024-01-27 06:34:23
  • jQuery 1.4新特性及其变化(上)

    2010-01-18 16:33:00
  • Django实现翻页的示例代码

    2023-01-10 23:10:20
  • Python数据结构之图的存储结构详解

    2021-03-28 10:42:48
  • python argparse传入布尔参数false不生效的解决

    2023-07-03 16:12:20
  • mysql查询的控制语句图文详解

    2024-01-27 00:02:38
  • MySQL用truncate命令快速清空一个数据库中的所有表

    2024-01-18 16:05:29
  • 你还在 Select * 吗?

    2024-01-24 22:10:50
  • asp之家 网络编程 m.aspxhome.com