天天看点

matplotlib.pyplot.subplot

为当前图像添加一个子图。调用格式:

subplot(nrows, ncols, index, **kwargs)
subplot(pos, **kwargs)
subplot(ax)
           

 参数

nrows、ncols、index:指定添加的子图位置,表示在包含nrows行ncols的网格中占据第index个位置。index是从最左上角开始向右从1计数的。

pos:作用与nrows、ncols、index相同。pos是一个三位的整数,三位分别代表了行数nrows、列数ncols以及索引index。但要注意的是使用pos时,三个数字必须都是小于10的。subplot(2,3,1)与subplot(231)是等效的。

projection:可选的,子图的投影类型。可选值有:aitoff、hammer、lambert、mollweide、polar、rectilinear。默认值为None,使用的是rectilinear。此外可以在注册自定义的projection后,传入自定义的projection名称。

polar:可选的,布尔值。如果为True,等价于projection为polar。

sharex、sharey:可选的,Axes。和sharex、sharey共享x、y轴。具有与共享的轴相同的范围、刻度和比例。

label:坐标轴的标签。

此外还接受基类Axes的关键字参数。

返回值

返回子图的坐标轴,matplotlib.axes.SubplotBase的子类或者Axes的子类,取决于使用的projection。

简单示例

#!/usr/bin/python
#coding=utf-8
import matplotlib.pyplot as plt

xdata=range(1,100)
ydata=[x*2+1 for x in xdata]
projections=["rectilinear","aitoff","hammer","lambert","mollweide","polar"]
fig=plt.figure(figsize=(10,8),dpi=100)
colsize=3
count=len(projections)
row=count//colsize + (count%colsize!=0 and 1 or 0 )

for i in range(count):
	print row,colsize,i+1
	ax=plt.subplot(row,colsize,i+1,projection=projections[i])
	ax.plot(xdata,ydata)
plt.show()
           

运行结果:

matplotlib.pyplot.subplot