Matplotlib画子图——subplot和subplots简单对比

前言

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

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

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

对比开始

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

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()#添加图列就是右上角的点说明

 

 

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()#添加图列就是右上角的点说明

对比结果

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

  • 15
    点赞
  • 64
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
subplotsubplots是matplotlib库中用于创建和管理子图的函数。 subplot函数的用法是matplotlib.pyplot.subplot(nrows, ncols, index, **kwargs),其中nrows和ncols分别表示子图的行数和列数,index表示当前子图的位置。通过这个函数可以在一个像中创建多个子图,并且可以使用不同的参数来设置每个子图的属性。 subplots函数的用法是matplotlib.pyplot.subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw),其中nrows和ncols分别表示子图的行数和列数。这个函数返回一个Figure实例和一个AxesSubplot实例的元组。可以通过这个函数一次性创建多个子图,并且可以使用不同的参数来设置每个子图的属性。 简而言之,subplot函数适用于创建单个子图,而subplots函数适用于一次性创建多个子图。它们都是用于在matplotlib中绘制多个图表的有用工具。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Matplotlibsubplot以及subplots的用法](https://blog.csdn.net/silent1cat/article/details/118753450)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值