subplot()功能将子图添加到指定网格位置处的当前图形。它类似于subplots()函数,但是与subplots()不同,它一次添加一个子图。因此,要创建多个图,您将需要使用subplot()函数的几行代码。子图功能的另一个缺点是,它会删除图形上先前存在的图。请参考示例1。

它是Figure.add_subplot的包装。

用法:

子图(nrows,ncols,index,** kwargs)

子图(pos,** kwargs)

子图(ax)

Parameters:

args:3位整数或三个单独的整数描述子图的位置。

pos是一个three-digit整数,其中第一个,第二个和第三个整数是nrows,ncols,index。

projection:[{None,“ aito ff”,“ hammer”,“ lambert”,“ mollweide”,“ polar”,“ rectalinear”,str},可选)。子图的projection-type(轴)。默认值为“无”会导致“直线”投影。

label:[str]返回轴的标签。

**kwargs:此方法还为返回的轴基类采用关键字参数。

除了数字参数,例如facecolor。

返回值:轴的axes.SubplotBase子类或轴的子类。返回的轴基类取决于所使用的投影。

该功能的实现如下:

范例1:subplot()将删除先前存在的绘图。

Python3

# importing hte module
import matplotlib.pyplot as plt
# Data to display on plot
x = [1, 2, 3, 4, 5]
y = [1, 2, 1, 2, 1]
# plot() will create new figure and will add axes object (plot) of above data
plt.plot(x, y, marker="x", color="green")
# subplot() will add plot to current figure deleting existing plot
plt.subplot(121)

输出:我们可以看到,第一个绘图被subplot()功能搁置了。

python subplot 间隔 python subplot函数_子类

subplot_gfg

如果要查看第一幅图,请注释出plt.subplot()行,您将看到以下图

python subplot 间隔 python subplot函数_子类_02

plot_gfg

范例2:

Python3
import matplotlib.pyplot as plt
# data to display on plots
x = [3, 1, 3]
y = [3, 2, 1]
z = [1, 3, 1]
# Creating figure object
plt.figure()
# addind first subplot
plt.subplot(121)
plt.plot(x, y, color="orange", marker="*")
# addding second subplot
plt.subplot(122)
plt.plot(z, y, color="yellow", marker="*")

输出:

python subplot 间隔 python subplot函数_python subplot 间隔_03

multiple_subplots