Python之Matplotlib实现subplot和subplots简单对比
内容摘要
这篇文章主要为大家详细介绍了Python之Matplotlib实现subplot和subplots简单对比,具有一定的参考价值,可以用来参考一下。
感兴趣的小伙伴,下面一起跟随php教程的雯雯来看看吧
感兴趣的小伙伴,下面一起跟随php教程的雯雯来看看吧
文章正文
这篇文章主要为大家详细介绍了Python之Matplotlib实现subplot和subplots简单对比,具有一定的参考价值,可以用来参考一下。
感兴趣的小伙伴,下面一起跟随php教程的雯雯来看看吧!
前言:
大家一般都知道subplot可以画子图,但是subplots也可以画子图,鉴于subplots介绍比较少,这里做一个对比,两者没有功能一致。
对比开始:
需求:画出两张子图,在一行显示,子图中的内容一模一样
subplot代码:
代码如下:
1 2 3 4 5 6 7 8 9 | <code> 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()#添加图列就是右上角的点说明</code> |
Matplotlib实现subplot和subplots简单对比
subplots代码
代码如下:
1 2 3 4 5 6 7 8 | <code> 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()#添加图列就是右上角的点说明</code> |
Matplotlib实现subplot和subplots简单对比
对比结果:
可以看出来两者都可以实现画子图功能,只不过subplots帮我们把画板规划好了,返回一个坐标数组对象,而subplot每次只能返回一个坐标对象,subplots可以直接指定画板的大小。
到此这篇关于Matplotlib实现subplot和subplots简单对比的文章就介绍到这了,更多相关Matplotlib subplot和subplots内容请搜索php教程以前的文章或继续浏览下面的相关文章希望大家以后多多支持php教程!
注:关于Python之Matplotlib实现subplot和subplots简单对比的内容就先介绍到这里,更多相关文章的可以留意
代码注释