Matplotlib實現(xiàn)subplot和subplots簡單對比
前言:
大家一般都知道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()#添加圖列就是右上角的點說明
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()#添加圖列就是右上角的點說明
對比結果:
可以看出來兩者都可以實現(xiàn)畫子圖功能,只不過subplots幫我們把畫板規(guī)劃好了,返回一個坐標數(shù)組對象,而subplot每次只能返回一個坐標對象,subplots可以直接指定畫板的大小。
參考博客:subplots與figure函數(shù)參數(shù)解釋說明以及簡單的使用腳本實例
到此這篇關于Matplotlib實現(xiàn)subplot和subplots簡單對比的文章就介紹到這了,更多相關Matplotlib subplot和subplots內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python導入Excel數(shù)據(jù)表的幾種實現(xiàn)方式
在Python中可以使用許多庫來處理Excel文件,下面這篇文章主要給大家介紹了關于Python導入Excel數(shù)據(jù)表的幾種實現(xiàn)方式,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-01-01利用Python計算質數(shù)與完全數(shù)的方法實例
這篇文章主要介紹了利用Python計算質數(shù)與完全數(shù)的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03Python 搭建Web站點之Web服務器網(wǎng)關接口
本文是Python 搭建Web站點系列文章的第二篇,接上文,主要給大家來講述Web服務器網(wǎng)關接口WSGI的相關資料,非常詳細,有需要的小伙伴可以參考下2016-11-11