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), orSubplotSpec
, default: (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
。
- 投影{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 的标签。
- *args : int, (int, int, index), 或
- 返回:
Axes
子图的轴。返回的轴实际上可以是子类的实例,例如极坐标投影的
projections.polar.PolarAxes
。
- 其他参数:
- **kwargs
此方法还接受返回的 Axes 基类的关键字参数;除了 figure 参数。直角坐标系基类
Axes
的关键字参数可以在下表中找到,但如果使用其他投影,可能还会有其他关键字参数。属性
描述
{'box', 'datalim'}
一个过滤函数,它接受一个 (m, n, 3) 浮点数组和一个 dpi 值,并返回一个 (m, n, 3) 数组和图像左下角的两个偏移量
标量或无
(float, float)
或{'C', 'SW', 'S', 'SE', 'E', 'NE', ...}
布尔
{'auto', 'equal'} 或 float
布尔
未知
未知
Callable[[Axes, Renderer], Bbox]
布尔值或 'line'
浮点数或无
BboxBase
或 None布尔
补丁或(路径,变换)或无
facecolor
或 fc布尔值或“自动”
布尔
str
布尔
对象
布尔
布尔
未知
None 或 bool 或 float 或 callable
[左, 底, 宽, 高] 或
Bbox
浮点数或无
布尔
(scale: float, length: float, randomness: float)
布尔值或无
未知
str
str
布尔
(lower: float, upper: float)
str
(左: 浮点数, 右: 浮点数)
浮点数大于 -0.5
未知
未知
未知
(lower: float, upper: float)
str
(底部: 浮点数, 顶部: 浮点数)
浮点数大于 -0.5
未知
未知
未知
浮动
注释
创建一个新的 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)