matplotlib.figure.SubFigure.add_subplot#

SubFigure.add_subplot(*args, **kwargs)[源代码]#

向图中添加一个 Axes,作为子图布局的一部分。

调用签名

add_subplot(nrows, ncols, index, **kwargs)
add_subplot(pos, **kwargs)
add_subplot(ax)
add_subplot()
参数:
*argsint, (int, int, index), 或 SubplotSpec, 默认值: (1, 1, 1)

由以下之一描述的子图位置

  • 三个整数 (nrows, ncols, index)。子图将占据一个具有 nrows 行和 ncols 列的网格中的 index 位置。index 从左上角开始计数,为 1,然后向右递增。index 也可以是一个二元组,指定子图的 (first, last) 索引(基于 1,包含 last),例如,fig.add_subplot(3, 1, (1, 2)) 会创建一个跨越图形顶部 2/3 的子图。

  • 一个三位整数。这些数字被解释为分别给出的三个单数字整数,即 fig.add_subplot(235)fig.add_subplot(2, 3, 5) 相同。请注意,此用法仅适用于子图数量不超过 9 个的情况。

  • 一个 SubplotSpec

在极少数情况下,add_subplot 可以使用单个参数调用,该参数是已在当前图中创建但不在图的 Axes 列表中的子图 Axes 实例。

projection{None, 'aitoff', 'hammer', 'lambert', 'mollweide', 'polar', 'rectilinear', str}, 可选

子图的投影类型 (Axes)。str 是自定义投影的名称,详见 projections。默认值 None 会生成 'rectilinear'(直角坐标系)投影。

polarbool, 默认值: False

如果为 True,相当于 projection='polar'。

axes_classAxes 的子类类型, 可选的

被实例化的 axes.Axes 子类。此参数与 projectionpolar 不兼容。示例请参阅 axisartist

sharex, shareyAxes, 可选的

与 sharex 和/或 sharey 共享 x 或 y 。该轴将与共享 Axes 的轴具有相同的限制、刻度和比例。

labelstr

返回的 Axes 的标签。

返回:
Axes

子图的 Axes。返回的 Axes 实际上可以是子类的实例,例如用于极坐标投影的 projections.polar.PolarAxes

其他参数:
**kwargs

此方法也接受返回的 Axes 基类的关键字参数;除了 figure 参数。直线坐标系基类 Axes 的关键字参数可以在下表中找到,但如果使用其他投影,则可能还有其他关键字参数。

属性

描述

adjustable

{'box', 'datalim'}

agg_filter

一个过滤函数,它接受一个 (m, n, 3) 浮点数组和一个 dpi 值,并返回一个 (m, n, 3) 数组以及图像左下角的两个偏移量

alpha

浮点数或 None

anchor

(float, float) 或 {'C', 'SW', 'S', 'SE', 'E', 'NE', ...}

animated

布尔值

aspect

{'auto', 'equal'} 或 float

autoscale_on

布尔值

autoscalex_on

未知

autoscaley_on

未知

axes_locator

Callable[[Axes, Renderer], Bbox]

axisbelow

布尔值 或 'line'

box_aspect

浮点数或 None

clip_box

BboxBase 或 None

clip_on

布尔值

clip_path

Patch 或 (Path, Transform) 或 None

facecolor or fc

color

figure

Figure or SubFigure

forward_navigation_events

bool 或 "auto"

frame_on

布尔值

gid

str

in_layout

布尔值

label

object

mouseover

布尔值

navigate

布尔值

navigate_mode

未知

path_effects

list of AbstractPathEffect

picker

None 或 布尔值 或 浮点数 或 可调用对象

position

[left, bottom, width, height] 或 Bbox

prop_cycle

Cycler

rasterization_zorder

浮点数或 None

rasterized

布尔值

sketch_params

(scale: 浮点数, length: 浮点数, randomness: 浮点数)

snap

布尔值或 None

subplotspec

未知

title

str

transform

变换

url

str

visible

布尔值

xbound

(lower: float, upper: float)

xlabel

str

xlim

(left: float, right: float)

xmargin

float 大于 -0.5

xscale

未知

xticklabels

未知

xticks

未知

ybound

(lower: float, upper: float)

ylabel

str

ylim

(bottom: float, top: float)

ymargin

float 大于 -0.5

yscale

未知

yticklabels

未知

yticks

未知

zorder

浮点数

示例

fig = plt.figure()

fig.add_subplot(231)
ax1 = fig.add_subplot(2, 3, 1)  # equivalent but more general

fig.add_subplot(232, frameon=False)  # subplot with no frame
fig.add_subplot(233, projection='polar')  # polar subplot
fig.add_subplot(234, sharex=ax1)  # subplot sharing x-axis with ax1
fig.add_subplot(235, facecolor="red")  # red subplot

ax1.remove()  # delete ax1 from the figure
fig.add_subplot(ax1)  # add ax1 back to the figure