其他分享
首页 > 其他分享> > 莫烦课程笔记总结之matplotlib(十二)——Subplot 分格显示

莫烦课程笔记总结之matplotlib(十二)——Subplot 分格显示

作者:互联网

Subplot 分格显示

 1 import matplotlib.pyplot as plt
 2 import matplotlib.gridspec as gridspec
 3 import numpy as np
 4 
 5 #method 1:subplot2grid
 6 #############################
 7 plt.figure()
 8 #分成3行3列,从0行0列开始,跨度1行 3列
 9 ax1 = plt.subplot2grid((3,3),(0,0),colspan=3,rowspan=1)
10 ax1.plot([1,2],[1,2])
11 ax1.set_title('ax1_title')
12 
13 ax2 = plt.subplot2grid((3,3),(1,0),colspan=2,rowspan=1)
14 ax3 = plt.subplot2grid((3,3),(1,2),colspan=2,rowspan=2)
15 ax4 = plt.subplot2grid((3,3),(2,0),colspan=1,rowspan=1)
16 ax5 = plt.subplot2grid((3,3),(2,1),colspan=1,rowspan=1)

运行结果

 

 

 1 import matplotlib.pyplot as plt
 2 import matplotlib.gridspec as gridspec
 3 
 4 #method 2:subplot2grid
 5 #############################
 6 
 7 plt.figure()
 8 gs = gridspec.GridSpec(3,3) #3行3列
 9 ax1 = plt.subplot(gs[0,:])
10 ax2 = plt.subplot(gs[1,:2])
11 ax3 = plt.subplot(gs[1:,2])
12 ax4 = plt.subplot(gs[-1,0])
13 ax5 = plt.subplot(gs[-1,-2])

运行结果:

 

 

1 import matplotlib.pyplot as plt
2 #method 3:subplot2grid
3 #############################
4 
5 f,((ax11,ax12),(ax21,ax22)) = plt.subplots(2,2,sharex=True,sharey=True)
6 ax11.scatter([1,2],[1,2])
7 
8 plt.tight_layout()
9 plt.show()

运行结果:

 

标签:Subplot,gridspec,plt,gs,莫烦,subplot2grid,分格,matplotlib,import
来源: https://www.cnblogs.com/industrial-fd-2019/p/14825297.html