matplotlib.pyplot.subplot#

matplotlib.pyplot.subplot(*args, **kwargs)[源代码][源代码]#

向当前图形添加一个坐标轴或检索一个现有的坐标轴。

这是 Figure.add_subplot 的一个包装器,在使用隐式 API 时提供额外的行为(参见注释部分)。

调用签名:

subplot(nrows, ncols, index, **kwargs)
subplot(pos, **kwargs)
subplot(**kwargs)
subplot(ax)
参数:
*args : int, (int, int, index), 或 SubplotSpec, 默认: (1, 1, 1)int, (int, int, index), or SubplotSpec, default: (1, 1, 1)

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

  • 三个整数(nrowsncolsindex)。子图将占据一个具有*nrows*行和*ncols*列的网格中的*index*位置。index*从左上角的1开始,并向右增加。*index*也可以是一个二元组,指定子图的(*firstlast)索引(基于1,并包括*last*),例如,fig.add_subplot(3, 1, (1, 2)) 创建一个跨越图表上部2/3的子图。

  • 一个三位整数。这些数字被解释为如果分别给出为三个一位整数,即 fig.add_subplot(235) 等同于 fig.add_subplot(2, 3, 5)。请注意,这只能用于不超过9个子图的情况。

  • 一个 SubplotSpec

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

子图的投影类型 (Axes)。str 是自定义投影的名称,参见 projections。默认值 None 会导致 'rectilinear' 投影。

极地bool, 默认值: False

如果为真,等同于 projection='polar'。

sharex, sharey : Axes, 可选轴,可选

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

标签str

返回的 Axes 的标签。

返回:
Axes

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

其他参数:
**kwargs

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

属性

描述

可调整

{'box', 'datalim'}

agg_filter

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

alpha

标量或无

锚点

(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

浮点数或无

clip_box

BboxBase 或 None

clip_on

布尔

clip_path

补丁或(路径,变换)或无

facecolor 或 fc

color

figure

FigureSubFigure

forward_navigation_events

布尔值或“自动”

frame_on

布尔

gid

str

in_layout

布尔

标签

对象

鼠标悬停

布尔

导航

布尔

navigate_mode

未知

path_effects

AbstractPathEffect 列表

picker

None 或 bool 或 float 或 callable

position

[左, 底, 宽, 高] 或 Bbox

prop_cycle

Cycler

rasterization_zorder

浮点数或无

光栅化

布尔

sketch_params

(scale: float, length: float, randomness: float)

snap

布尔值或无

subplotspec

未知

标题

str

变换

Transform

url

str

可见

布尔

xbound

(lower: float, upper: float)

xlabel

str

xlim

(左: 浮点数, 右: 浮点数)

xmargin

浮点数大于 -0.5

xscale

未知

xticklabels

未知

xticks

未知

ybound

(lower: float, upper: float)

ylabel

str

ylim

(底部: 浮点数, 顶部: 浮点数)

ymargin

浮点数大于 -0.5

yscale

未知

yticklabels

未知

yticks

未知

zorder

浮动

注释

创建一个新的 Axes 将删除任何与其重叠的现有 Axes,除非它们共享一个边界:

import matplotlib.pyplot as plt
# plot a line, implicitly creating a subplot(111)
plt.plot([1, 2, 3])
# now create a subplot which represents the top plot of a grid
# with 2 rows and 1 column. Since this subplot will overlap the
# first, the plot (and its Axes) previously created, will be removed
plt.subplot(211)

如果你不想要这种行为,请使用 Figure.add_subplot 方法或 pyplot.axes 函数代替。

如果没有传递 kwargs 并且存在位于 args 指定位置的 Axes,则将返回该 Axes 而不是创建新的 Axes。

如果传递了 kwargs 并且存在位于 args 指定位置的 Axes,投影类型相同,并且 kwargs 与现有 Axes 匹配,则返回现有 Axes。否则,使用指定参数创建一个新的 Axes。我们保存一个 kwargs 的引用,用于此比较。如果 kwargs 中的任何值是可变的,我们将无法检测到它们被修改的情况。在这些情况下,我们建议使用 Figure.add_subplot 和显式的 Axes API,而不是隐式的 pyplot API。

示例

plt.subplot(221)

# equivalent but more general
ax1 = plt.subplot(2, 2, 1)

# add a subplot with no frame
ax2 = plt.subplot(222, frameon=False)

# add a polar subplot
plt.subplot(223, projection='polar')

# add a red subplot that shares the x-axis with ax1
plt.subplot(224, sharex=ax1, facecolor='red')

# delete ax2 from the figure
plt.delaxes(ax2)

# add ax2 to the figure again
plt.subplot(ax2)

# make the first Axes "current" again
plt.subplot(221)

使用 matplotlib.pyplot.subplot 的示例#

使用边距和粘性边缘控制视图限制

Controlling view limits using margins and sticky_edges

使用紧凑布局调整坐标轴大小

Resize Axes with tight layout

地理投影

Geographic Projections

在 pyplot 中管理多个图形

Manage multiple figures in pyplot

共享轴限制和视图

Share axis limits and views

共享轴

Shared axis

多个子图

Multiple subplots

子图间距和边距

Subplots spacings and margins

极坐标轴上的条形图

Bar chart on polar axis

使用 pyplot 绘制两个子图

Two subplots using pyplot

Matplotlib 解禁

Matplotlib unchained

自定义 Rc

Customize Rc

transforms.offset_copy

transforms.offset_copy

Pyplot 教程

Pyplot tutorial

约束布局指南

Constrained layout guide

紧凑布局指南

Tight layout guide

在图形中排列多个轴

Arranging multiple Axes in a Figure