语法:
subplot(*args, **kwargs)
类型:
matplotlib.pyplot模块中的函数,返回一条位于给定网格位置的Axes(字面意思是轴)。
需要的特点:
subplot(nrows, ncols, index, **kwargs)
在当前的图片,创建并返回一个Axes,在一个虚拟的nrows*ncols网格中的index
位置,索引从1到nrows**ncols,索引沿着行优先顺序增加。如果nrows,ncols和index都小于10,它们也可以作为一个单一的,连续的三位数。
举个例子,subplot(2, 3, 3) 和subplot(233)都可以创早一个Axes,在当前图片的右上角,占据图像高度的一半和宽度的三分之一。
分析:nrow=2 ,ncol =3,一共6个图片,以行为主进行标记。则所有的标号如下,index = 3,所以是右上角的图片
1 | 2 | 3 |
---|---|---|
4 | 5 | 6 |
笔记
创建一个子图,这个子图会删除之前存在的子图,之前存在的子图在共享边界之外与它重合。
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)
plt.plot(range(12))
plt.subplot(212, facecolor='y')
# creates 2nd subplot with yellow background
如果你不想要这种结果,使用add_subplot() 方法或者axes()方法替换。
关键参数:
facecolor:
子图的背景颜色,这个是可以任意一个合法的颜色指定器,参看matplotlib.colors 获得更多信息
polar:
一个布尔型标志,表示子图是否应该是一个极投影,默认为False
projection:
一个字符串,给定了自定义的可以用于子图的设计的名字。这个设计必须之前已经注册过的,参见 matplotlib.projections.
还可以看:
axes():额外的和axes()和subplot()关键参数的额外信息。
gallery/pie_and_polar_charts/polar_scatter.py:例子