網站首頁 編程語言 正文
本文速覽
matplotlib.pyplot api 繪制子圖
面向對象方式繪制子圖
matplotlib.gridspec.GridSpec繪制子圖
任意位置添加子圖
關于pyplot和面向對象兩種繪圖方式可參考之前文章:matplotlib.pyplot api verus matplotlib object-oriented
1、matplotlib.pyplot api 方式添加子圖
import?matplotlib.pyplot?as?plt
my_dpi=96
plt.figure(figsize=(480/my_dpi,480/my_dpi),dpi=my_dpi)
plt.subplot(221)
plt.plot([1,2,3])
plt.subplot(222)
plt.bar([1,2,3],[4,5,6])
plt.title('plt.subplot(222)')#注意比較和上面面向對象方式的差異
plt.xlabel('set_xlabel')
plt.ylabel('set_ylabel',fontsize=15,color='g')#設置y軸刻度標簽
plt.xlim(0,8)#設置x軸刻度范圍
plt.xticks(range(0,10,2))???#?設置x軸刻度間距
plt.tick_params(axis='x',?labelsize=20,?rotation=45)#x軸標簽旋轉、字號等
plt.subplot(223)
plt.plot([1,2,3])
plt.subplot(224)
plt.bar([1,2,3],[4,5,6])
plt.suptitle('matplotlib.pyplot?api',color='r')
fig.tight_layout(rect=(0,0,1,0.9))
plt.subplots_adjust(left=0.125,
????????????????????bottom=-0.51,
????????????????????right=1.3,
????????????????????top=0.88,
????????????????????wspace=0.2,
????????????????????hspace=0.2
???????????????????)
???????????????????
#plt.tight_layout()
plt.show()
2、面向對象方式添加子圖
import?matplotlib.pyplot?as?plt
my_dpi=96
fig,?axs?=?plt.subplots(2,2,figsize=(480/my_dpi,480/my_dpi),dpi=my_dpi,
???????????????????????sharex=False,#x軸刻度值共享開啟
???????????????????????sharey=False,#y軸刻度值共享關閉????????????????????????
????????????????????????
???????????????????????)
#fig為matplotlib.figure.Figure對象
#axs為matplotlib.axes.Axes,把fig分成2x2的子圖
axs[0][0].plot([1,2,3])
axs[0][1].bar([1,2,3],[4,5,6])
axs[0][1].set(title='title')#設置axes及子圖標題
axs[0][1].set_xlabel('set_xlabel',fontsize=15,color='g')#設置x軸刻度標簽
axs[0][1].set_ylabel('set_ylabel',fontsize=15,color='g')#設置y軸刻度標簽
axs[0][1].set_xlim(0,8)#設置x軸刻度范圍
axs[0][1].set_xticks(range(0,10,2))???#?設置x軸刻度間距
axs[0][1].tick_params(axis='x',?#可選'y','both'
??????????????????????labelsize=20,?rotation=45)#x軸標簽旋轉、字號等
axs[1][0].plot([1,2,3])
axs[1][1].bar([1,2,3],[4,5,6])
fig.suptitle('matplotlib?object-oriented',color='r')#設置fig即整整張圖的標題
#修改子圖在整個figure中的位置(上下左右)
plt.subplots_adjust(left=0.125,
????????????????????bottom=-0.61,
????????????????????right=1.3,#防止右邊子圖y軸標題與左邊子圖重疊
????????????????????top=0.88,
????????????????????wspace=0.2,
????????????????????hspace=0.2
???????????????????)
#?參數介紹
'''
##?The?figure?subplot?parameters.??All?dimensions?are?a?fraction?of?the?figure?width?and?height.
#figure.subplot.left:???0.125??#?the?left?side?of?the?subplots?of?the?figure
#figure.subplot.right:??0.9????#?the?right?side?of?the?subplots?of?the?figure
#figure.subplot.bottom:?0.11???#?the?bottom?of?the?subplots?of?the?figure
#figure.subplot.top:????0.88???#?the?top?of?the?subplots?of?the?figure
#figure.subplot.wspace:?0.2????#?the?amount?of?width?reserved?for?space?between?subplots,
???????????????????????????????#?expressed?as?a?fraction?of?the?average?axis?width
#figure.subplot.hspace:?0.2????#?the?amount?of?height?reserved?for?space?between?subplots,
???????????????????????????????#?expressed?as?a?fraction?of?the?average?axis?height
'''
plt.show()
3、matplotlib.pyplot add_subplot方式添加子圖
my_dpi=96
fig?=?plt.figure(figsize=(480/my_dpi,480/my_dpi),dpi=my_dpi)
fig.add_subplot(221)
plt.plot([1,2,3])
fig.add_subplot(222)
plt.bar([1,2,3],[4,5,6])
plt.title('fig.add_subplot(222)')
fig.add_subplot(223)
plt.plot([1,2,3])
fig.add_subplot(224)
plt.bar([1,2,3],[4,5,6])
plt.suptitle('matplotlib.pyplot?api:add_subplot',color='r')
4、matplotlib.gridspec.GridSpec方式添加子圖
語法:matplotlib.gridspec.GridSpec(nrows, ncols, figure=None, left=None, bottom=None, right=None, top=None, wspace=None, hspace=None, width_ratios=None, height_ratios=None)
import?matplotlib.pyplot?as?plt
from?matplotlib.gridspec?import?GridSpec
fig?=?plt.figure(dpi=100,
?????????????????constrained_layout=True,#類似于tight_layout,使得各子圖之間的距離自動調整【類似excel中行寬根據內容自適應】
?????????????????
????????????????)
gs?=?GridSpec(3,?3,?figure=fig)#GridSpec將fiure分為3行3列,每行三個axes,gs為一個matplotlib.gridspec.GridSpec對象,可靈活的切片figure
ax1?=?fig.add_subplot(gs[0,?0:1])
plt.plot([1,2,3])
ax2?=?fig.add_subplot(gs[0,?1:3])#gs[0,?0:3]中0選取figure的第一行,0:3選取figure第二列和第三列
#ax3?=?fig.add_subplot(gs[1,?0:2])
plt.subplot(gs[1,?0:2])#同樣可以使用基于pyplot?api的方式
plt.scatter([1,2,3],[4,5,6],marker='*')
ax4?=?fig.add_subplot(gs[1:3,?2:3])
plt.bar([1,2,3],[4,5,6])
ax5?=?fig.add_subplot(gs[2,?0:1])
ax6?=?fig.add_subplot(gs[2,?1:2])
fig.suptitle("GridSpec",color='r')
plt.show()
5、子圖中繪制子圖
import?matplotlib.pyplot?as?plt
import?matplotlib.gridspec?as?gridspec
def?format_axes(fig):
????for?i,?ax?in?enumerate(fig.axes):
????????ax.text(0.5,?0.5,?"ax%d"?%?(i+1),?va="center",?ha="center")
????????ax.tick_params(labelbottom=False,?labelleft=False)
#?子圖中再繪制子圖
fig?=?plt.figure(dpi=100,
????????????????constrained_layout=True,
????????????????)
gs0?=?GridSpec(1,?2,?figure=fig)#將figure切片為1行2列的兩個子圖
gs00?=?gridspec.GridSpecFromSubplotSpec(3,?3,?subplot_spec=gs0[0])#將以上第一個子圖gs0[0]再次切片為3行3列的9個axes
#gs0[0]子圖自由切片
ax1?=?fig.add_subplot(gs00[:-1,?:])
ax2?=?fig.add_subplot(gs00[-1,?:-1])
ax3?=?fig.add_subplot(gs00[-1,?-1])
gs01?=?gs0[1].subgridspec(3,?3)#將以上第二個子圖gs0[1]再次切片為3行3列的axes
#gs0[1]子圖自由切片
ax4?=?fig.add_subplot(gs01[:,?:-1])
ax5?=?fig.add_subplot(gs01[:-1,?-1])
ax6?=?fig.add_subplot(gs01[-1,?-1])
plt.suptitle("GridSpec?Inside?GridSpec",color='r')
format_axes(fig)
plt.show()
6、任意位置繪制子圖(plt.axes)
plt.subplots(1,2,dpi=100)
plt.subplot(121)
plt.plot([1,2,3])
plt.subplot(122)
plt.plot([1,2,3])
plt.axes([0.7,?0.2,?0.15,?0.15],?##?[left,?bottom,?width,?height]四個參數(fractions?of?figure)可以非常靈活的調節子圖中子圖的位置?????
????????)
plt.bar([1,2,3],[1,2,3],color=['r','b','g'])
plt.axes([0.2,?0.6,?0.15,?0.15],?
????????)
plt.bar([1,2,3],[1,2,3],color=['r','b','g'])
原文鏈接:https://mp.weixin.qq.com/s/SeqyfzNBO_mcGJbPLHbjlQ
相關推薦
- 2022-10-22 Android使用ViewStub實現布局優化方法示例_Android
- 2022-07-21 vscode代碼保存時自動格式化
- 2022-10-31 Kotlin中Object關鍵字的使用示例介紹_Android
- 2022-08-06 C語言實現UDP通信_C 語言
- 2022-08-18 Python如何獲取實時股票信息的方法示例_python
- 2022-05-12 tp6 集成swoole
- 2022-05-27 使用Jedis線程池returnResource異常注意事項_Redis
- 2023-03-01 pyinstaller打包后偶爾出現黑窗口一閃而過的問題及解決_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同步修改后的遠程分支