Matplotlib中subplot和subplots绘制子图区别在哪?
发布时间:2022-03-02 13:06:53 所属栏目:语言 来源:互联网
导读:Matplotlib中subplot和subplots绘制子图区别在哪?我们知道subplot和subplots都可以绘制子图,但是一些朋友可能对两者的区别不是很了解,下面我们就通过一个实例来对比看看subplot和subplots绘制子图的不同。 对比开始: 需求:画出两张子图,在一行显示,子
Matplotlib中subplot和subplots绘制子图区别在哪?我们知道subplot和subplots都可以绘制子图,但是一些朋友可能对两者的区别不是很了解,下面我们就通过一个实例来对比看看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可以直接指定画板的大小。 ![]() (编辑:帝国网站管理系统) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |