Python中的 Matplotlib.gridspec.GridSpec 类
Matplotlib是Python中用于数组二维图的惊人可视化库。 Matplotlib 是一个基于 NumPy 数组构建的多平台数据可视化库,旨在与更广泛的 SciPy 堆栈配合使用。
matplotlib.gridspec.GridSpec
matplotlib.gridspec.GridSpec 类用于指定网格的几何形状以放置子图。为此,必须设置行数和列数。或者,也可以调整子图布局参数。
Syntax: class 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)
Parameters:
- nrows: It is an integer representing the number of rows in the grid.
- ncols: It is an integer representing the number of columns in the grid.
- figure: It is an optional parameter used to draw figures.
- left, right, top, bottom: These are optional parameters used to define the extent of the subplots as fraction of figure width or height.
- wspase: It is an optional float argument used to reserve the width space between subplots.
- hspace: It is an optional float argument used to reserve the height space between subplots.
- width_ratios: It is an optional parameter that represents the width ratios of the columns.
- height_ratios: It is an optional parameter that represents the width ratios of the rows.
类的方法:
- get_subplot_params(self, figure=None):它返回子图布局参数的字典。除非设置了图形属性,否则默认参数来自 rcParams。
- ight_layout(self, figure, renderer=None, pad=1.08, h_pad=None, w_pad=None, rect=None):用于给定内边距以调整子图。这里 pad 是一个浮点值,它将图形边缘和子图边缘之间的填充设置为字体大小的一部分。 h_pad 和 w_pad 是可选参数,用于设置相邻子图之间的填充。 rect 还用于规范化包含所有子图区域的矩形的图形坐标。它的默认值为 (0, 0, 1, 1)。它是 4 个浮点数的元组。
示例 1:
Python3
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
gs = GridSpec(8, 39)
ax1 = plt.subplot(gs[:6, :35])
ax2 = plt.subplot(gs[6:, :])
data1 = np.random.rand(6, 35)
data2 = np.random.rand(2, 39)
ax1.imshow(data1)
ax2.imshow(data2)
plt.show()
Python3
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
fig = plt.figure(figsize =([7, 4]))
gs = gridspec.GridSpec(2, 6)
gs.update(wspace = 1.5, hspace = 0.3)
ax1 = plt.subplot(gs[0, :2])
ax1.set_ylabel('ylabel', labelpad = 0, fontsize = 12)
ax2 = plt.subplot(gs[0, 2:4])
ax2.set_ylabel('ylabel', labelpad = 0, fontsize = 12)
ax3 = plt.subplot(gs[0, 4:6])
ax3.set_ylabel('ylabel', labelpad = 0, fontsize = 12)
ax4 = plt.subplot(gs[1, 1:3])
ax4.set_ylabel('ylabel', labelpad = 0, fontsize = 12)
ax5 = plt.subplot(gs[1, 3:5])
ax5.set_ylabel('ylabel', labelpad = 0, fontsize = 12)
plt.show()
输出:
示例 2:
Python3
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
fig = plt.figure(figsize =([7, 4]))
gs = gridspec.GridSpec(2, 6)
gs.update(wspace = 1.5, hspace = 0.3)
ax1 = plt.subplot(gs[0, :2])
ax1.set_ylabel('ylabel', labelpad = 0, fontsize = 12)
ax2 = plt.subplot(gs[0, 2:4])
ax2.set_ylabel('ylabel', labelpad = 0, fontsize = 12)
ax3 = plt.subplot(gs[0, 4:6])
ax3.set_ylabel('ylabel', labelpad = 0, fontsize = 12)
ax4 = plt.subplot(gs[1, 1:3])
ax4.set_ylabel('ylabel', labelpad = 0, fontsize = 12)
ax5 = plt.subplot(gs[1, 3:5])
ax5.set_ylabel('ylabel', labelpad = 0, fontsize = 12)
plt.show()
输出: