網站首頁 編程語言 正文
前段時間就遇到了這個問題,一直忘了寫,今晚夜深人靜總結一波~
問題
我相信,看到這篇博客的人,你肯定已經會使用Matplotlib中的pyplot畫圖。?
比如下面這種圖
?你也應該會調整單個圖的大小了,就是使用如下語句控制單個圖形figure的大小,比如我這里設的8*6的。
fig3 = plt.figure(figsize=(8,6))
但隨著繼續深入的學習,有時我們很有必要將兩個圖畫在一起,來做對比,所以你也應該會在一個畫布上畫多個子圖了。比如下圖
?即是通過subplot實現
#展示一下數據
fig = plt.figure(figsize=(15,7))
fig1 = plt.subplot(231)
plt.scatter(data.loc[:,'Avg. Area Income'],data.loc[:,'Price'])
plt.title('Income VS Price')
fig2 = plt.subplot(232)
plt.scatter(data.loc[:,'Avg. Area House Age'],data.loc[:,'Price'])
plt.title('Age VS Price')
fig3 = plt.subplot(233)
plt.scatter(data.loc[:,'Avg. Area Number of Rooms'],data.loc[:,'Price'])
plt.title('Number VS Price')
fig4 = plt.subplot(234)
plt.scatter(data.loc[:,'Area Population'],data.loc[:,'Price'])
plt.title('Population VS Price')
fig5 = plt.subplot(235)
plt.scatter(data.loc[:,'size'],data.loc[:,'Price'])
plt.title('size VS Price')
plt.show()
?目前為止圖好像沒有問題,那問題在哪呢?就是在子圖比較少的時候,整個圖可能會變形,出現下圖情況。
這顯然不是我們期望的,我們希望他不要拉長。但是貌似直接通過subplot畫出的子圖無法更改大小,網上給的一些方案也比較麻煩。
簡便的解決方法
?把這兩個子圖畫在同一個畫布里,這樣即是子圖無法改變,但是外面的畫布大小可以改變,子圖就可以根據外面畫布大小自適應的顯示了。
對于該圖
?其原始代碼為
fig6 = plt.subplot(121)
label0 = plt.scatter(X.loc[:,'V1'][y_corrected==0],X.loc[:,'V2'][y_corrected==0])
label1 = plt.scatter(X.loc[:,'V1'][y_corrected==1],X.loc[:,'V2'][y_corrected==1])
label2 = plt.scatter(X.loc[:,'V1'][y_corrected==2],X.loc[:,'V2'][y_corrected==2])
plt.title("corrected data")
plt.xlabel('V1')
plt.ylabel('V2')
plt.legend((label0,label1,label2),('label0','label1','label2'))
plt.scatter(centers[:,0],centers[:,1])
fig7 = plt.subplot(122)
label0 = plt.scatter(X.loc[:,'V1'][y==0],X.loc[:,'V2'][y==0])
label1 = plt.scatter(X.loc[:,'V1'][y==1],X.loc[:,'V2'][y==1])
label2 = plt.scatter(X.loc[:,'V1'][y==2],X.loc[:,'V2'][y==2])
plt.title("labled data")
plt.xlabel('V1')
plt.ylabel('V2')
plt.legend((label0,label1,label2),('label0','label1','label2'))
plt.scatter(centers[:,0],centers[:,1])
plt.show()
?可以看到兩個子圖fig6和fig7都是直接使用subplot得到的,所以它變形了。
修改后應該是這樣的:
對應代碼 :
fig = plt.figure(figsize=(11,4))
fig6 = plt.subplot(121)
label0 = plt.scatter(X.loc[:,'V1'][y_corrected==0],X.loc[:,'V2'][y_corrected==0])
label1 = plt.scatter(X.loc[:,'V1'][y_corrected==1],X.loc[:,'V2'][y_corrected==1])
label2 = plt.scatter(X.loc[:,'V1'][y_corrected==2],X.loc[:,'V2'][y_corrected==2])
plt.title("corrected data")
plt.xlabel('V1')
plt.ylabel('V2')
plt.legend((label0,label1,label2),('label0','label1','label2'))
plt.scatter(centers[:,0],centers[:,1])
fig7 = plt.subplot(122)
label0 = plt.scatter(X.loc[:,'V1'][y==0],X.loc[:,'V2'][y==0])
label1 = plt.scatter(X.loc[:,'V1'][y==1],X.loc[:,'V2'][y==1])
label2 = plt.scatter(X.loc[:,'V1'][y==2],X.loc[:,'V2'][y==2])
plt.title("labled data")
plt.xlabel('V1')
plt.ylabel('V2')
plt.legend((label0,label1,label2),('label0','label1','label2'))
plt.scatter(centers[:,0],centers[:,1])
plt.show()
?相比原來的代碼就多了第一行的操作,定一個合適畫布的大小就可以方便動態調整子圖了。
麻煩點的方法
?看到網上是有可以自定義子圖大小的方法的,不過相比我想出來的這個方法,感覺太麻煩了。這個方法能解決我這一類問題了,如果后面遇到需要一個子圖大一個子圖小的問題再單獨記錄把。
總結
原文鏈接:https://blog.csdn.net/doubleguy/article/details/120008554
相關推薦
- 2022-09-30 Docker容器搭建本地私有倉庫詳情_docker
- 2022-05-02 深入了解Python?中線程和進程區別_python
- 2022-07-04 Python繪制多因子柱狀圖的實現示例_python
- 2023-03-15 Python多進程協作模擬實現流程_python
- 2022-09-03 列表頁常見hook封裝實例_React
- 2022-07-28 Redis基本數據類型Set常用操作命令_Redis
- 2022-12-03 Flutter狀態管理Bloc使用示例詳解_Android
- 2023-01-05 Python?optparse模塊及簡單使用_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支