3、matplotlib 子图
程序员文章站
2022-07-13 11:02:42
...
matplotlib.pyplot.subplot
subplot(行数, 列数, 第n个图)
import matplotlib.pyplot as mp
mp.figure(facecolor='lightgray')
for i in range(4):
mp.subplot(2,2,i+1)
mp.xticks(())
mp.yticks(())
# .text(x, y, s, fontdict=None, withdash=False, **kwargs)
mp.text(0.5, 0.5, str(i+1), ha='center', va='center', size=36, alpha=0.5)
mp.tight_layout()
mp.show()
matplotlib.pyplot.axes
axes([左距, 下距, 宽, 高])
import matplotlib.pyplot as mp
mp.figure(facecolor='lightgray', figsize=(2, 2))
mp.axes([0.05, 0.05, 0.9, 0.9])
mp.xticks(())
mp.yticks(())
mp.text(0.5, 0.5, '1', ha='center', va='center', size=36, alpha=0.5)
mp.axes([0.1, 0.1, 0.3, 0.3])
mp.xticks(())
mp.yticks(())
mp.text(0.5, 0.5, '2', ha='center', va='center', size=36, alpha=0.5)
mp.show()
matplotlib.gridspec.GridSpec
GridSpec(行数, 列数)
import matplotlib.pyplot as mp
import matplotlib.gridspec as mg
# 创建figure对象
mp.figure(facecolor='lightgray')
# 3行3列
gs = mg.GridSpec(3, 3)
for i in range(3):
for j in range(3):
# 传入参数
mp.subplot(gs[i, j])
mp.xticks(())
mp.yticks(())
mp.text(0.5, 0.5, str(i)+str(j), ha='center', va='center', size=36, alpha=0.5)
mp.tight_layout()
mp.show()
import matplotlib.pyplot as mp
import matplotlib.gridspec as mg
# 创建figure对象
mp.figure(facecolor='lightgray')
# 3行3列
gs = mg.GridSpec(3, 3)
# 子图1
mp.subplot(gs[0, :2])
mp.xticks(())
mp.yticks(())
mp.text(0.5, 0.5, '1', ha='center', va='center', size=36, alpha=0.5)
# 子图2
mp.subplot(gs[1:, 0])
mp.xticks(())
mp.yticks(())
mp.text(0.5, 0.5, '2', ha='center', va='center', size=36, alpha=0.5)
# 子图3
mp.subplot(gs[2, 1:])
mp.xticks(())
mp.yticks(())
mp.text(0.5, 0.5, '3', ha='center', va='center', size=36, alpha=0.5)
# 子图4
mp.subplot(gs[:2, 2])
mp.xticks(())
mp.yticks(())
mp.text(0.5, 0.5, '4', ha='center', va='center', size=36, alpha=0.5)
# 子图5
mp.subplot(gs[1, 1])
mp.xticks(())
mp.yticks(())
mp.text(0.5, 0.5, '5', ha='center', va='center', size=36, alpha=0.5)
# 防挤压
mp.tight_layout()
# 展示
mp.show()