温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Matplotlib的subplot和subplots怎么使用

发布时间:2022-02-25 16:42:36 来源:亿速云 阅读:138 作者:iii 栏目:开发技术

这篇文章主要介绍了Matplotlib的subplot和subplots怎么使用的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Matplotlib的subplot和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()#添加图列就是右上角的点说明

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可以直接指定画板的大小。

关于“Matplotlib的subplot和subplots怎么使用”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“Matplotlib的subplot和subplots怎么使用”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI