- 1.subplot2grid()的使用方法
- 2.模块gridspec中的类GridSpec的使用方法
subplot2grid()函数的rowspan和colspan参数可以让子区跨越固定的网格布局的多个行和列,实现不同的子区布局,比上一种方法方便许多。 1.subplot2grid()的使用方法
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
mpl.rcParams["font.sans-serif"]=["SimHei"]
mpl.rcParams["axes.unicode_minus"]=False
# set subplot(23,1-2)
plt.subplot2grid((2,3),(0,0),colspan=2)
x = np.linspace(0.0,4.0,100)
y = np.random.randn(100)
plt.scatter(x,y,c="c")
plt.title("散点图")
# set subplot(233)
plt.subplot2grid((2,3),(0,2))
plt.title("空白绘图区域")
# set subplot(23,4-6)
plt.subplot2grid((2,3),(1,0),colspan=3)
x = np.linspace(0.0,4.0,100)
y1 = np.sin(x)
plt.plot(x,y1,lw=2,ls="-")
plt.xlim(0,3)
plt.grid(True,ls=":",c="r")
plt.title("折线图")
# set figure title
plt.suptitle("subplot2grid()函数的实例展示",fontsize=25)
plt.show()
我们通过调用函数subplot2grid(shape,loc),将参数shape所划定的网格布局作为绘图区域,实现在参数loc位置处绘制图形的目的。上面的代码中,参数shape设置了2行3列的网格布局,参数loc表示元组的第一个和第二个数字的起点都是0。
以plt.subplot2grid((2,3),(0,0),colspan=2)语句为例,参数loc=(0,0)就表示图形将第一行第一列作为位置起点,跨越两列。形影的,plt.subplot2grid((2,3),(0,2))语句就表示图形将第一行和第三列作为位置起点。值得注意的是,图形位置的索引起点是从0开始算起的,而不是像子区函数subplot()中的图形位置是从1开始算起的。
函数suptitle()是绘制Figure画布标题的文本内容,函数title()是绘制坐标轴Axes实例的图形标题的文本内容。
在matplotlib中,存在一个模块gridspec是一个可以指定画布中子区位置或者说是布局的“分区”模块。在模块gridspec中,有一个类GridSpec。类GridSpec可以指定网格的几何形状,也就是说,可以划定一个子区的网格状的几何结构。我们需要设定网格的行数和列数,以此确定子区的划分结构样式。
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
from matplotlib.gridspec import GridSpec
fig = plt.figure()
gs = GridSpec(2,2)
box = {"facecolor":"lightgreen","pad":3,"alpha":0.2}
# subplot(2,2,1-2)
x1 = np.arange(0,1e5,500)
ax1 = fig.add_subplot(gs[0,:],facecolor="yellowgreen")
ax1.plot(x1,"k--",lw=2)
ax1.set_ylabel('YLabel0,0-1',bbox=box)
ax1.set_xlabel('YLabel0,0-1',bbox=box)
ax1.yaxis.set_label_coords(-0.1,0.5)
# subplot(2,2,3)
x2 = np.linspace(0,1000,10)
y2 = np.arange(1,11,1)
ax2 = fig.add_subplot(gs[1,0],facecolor="cornflowerblue")
ax2.scatter(x2,y2,s=20,c="grey",marker="s",linewidths=2,edgecolors="k")
ax2.set_ylabel("Ylabel10",bbox=box)
ax2.set_xlabel("Xlable110",bbox=box)
for ticklabel in ax2.get_xticklabels():
ticklabel.set_rotation(45)
ax2.yaxis.set_label_coords(-0.25,0.5)
ax2.xaxis.set_label_coords(0.5,-0.25)
# subplot(2,2,4)
x3 = np.linspace(0,10,100)
y3 = np.exp(-x3)
ax3 = fig.add_subplot(gs[1,1])
ax3.errorbar(x3,y3,fmt="b-",yerr=0.6*y3,ecolor="lightsteelblue",elinewidth=2,
capsize=0,errorevery=5)
ax3.set_ylabel("YLabel11",bbox=box)
ax3.set_xlabel("YLabel11",bbox=box)
ax3.xaxis.set_label_coords(0.5,-0.25)
ax3.set_ylim(-0.1,1.1)
ax3.set_yticks(np.arange(0,1.1,0.1))
gs.tight_layout(fig)
plt.show()
plt.show()
通过语句GridSpec(2,2)生成一个2行2列的实例gs。
然后,我们要将画布划分成2行2列的网格状几何形状。实例方法add_subplot()的参数gs[0,:]表示第一行、全部列作为子区,参数gs的索引是从0开始算起,即gs[1,0]表示第2行第1列的子区位置。类GridSpec(2,2)和实例gs的子区位置索引等特点,与函数subplot2grid(shape,loc)是很类似的。参数shape设置了行列的网格布局,参数loc所表示的元组的第一个和第二个数字的索引起点都是从0开始的。因此根据实例gs的子区位置索引的特点,我们分别绘制了gs[1,0]和gs[1,1]位置的子区add_subplot()。在这三个子区里,我们分别绘制了折线图、散点图和误差棒图。
最后,我们为了使子区gs[0,:]和子区gs[1,0]的y轴坐标标签的位置对齐,调用实例方法set_label_coords(x,y)进行位置调整,参数x和参数y的默认坐标系统是Axes坐标系统。



