【Matplotlib-8】画布分割
title设置子区标题,suptitle设置总标题
(1)等分划分子区
subplot(C,R,P),那么这三个整数就表示在C行、R列的网格布局上,子区subplot()会被放置在第P个位置上,即为将被创建的子区编号,子区编号从1开始,起始于右上角,序号依次向右递增。
子区的编号从1开始。
P只表示编号,不代表实际有几个子区,根据编号确定子区的位置。
每个子区一个图
-
一次创建有若干若干子区的fig
fig,ax = plt.subplots(nrows, ncols)nrows,ncols——创建有几行几列子区的fig
fig——画布对象
ax——子区对象数组,nrows行,ncols列
fig, ax = plt.subplots(1,2,sharey=True) ## 一次创建有1行2列子区的画布,返回fig画布实例、ax是子区实例的集合 ax1 = ax[0] ## 第1个子区 ax1.plot(x,y,"k--",lw=2) ax1.set_title(’折线图’) ax1.grid(ls=":",lw=1,color="gray",alpha=0.8) ax2 = ax[1] ## 第2个子区 ax2.scatter(x,y,s=10,c="skyblue",marker="o") ax2.set_title("散点图")如果我们想要改变子区边缘相距画布边缘的距离和子区边缘之间的高度与宽度的距离,可以调用函数subplots_adjust(*agrs,**kwargs)进行设置。
import matplotlib.pyplot as plt import numpy as np # fig = plt.figure() fig,ax = plt.subplots(2,3,sharey=True) x = np.linspace(0.0,2*np.pi) y = np.cos(x)*np.sin(x) # ax1 = fig.add_subplot(211) ax1 = ax[0,0] ## 第1行第1列子区 ax1.margins(0.03) ax1.set_title("11111", fontsize =12) ax1.plot(x,y,ls="-",lw=2,color="b") # ax2 = fig.add_subplot(223) ax2 = ax[0,1] ## 第1行第2列子区 ax2.margins(0.7,0.7) ax2.set_title("22222", fontsize =12) ax2.plot(x,y,ls="-",lw=2,color="r") # ax3 = fig.add_subplot(224) ax3 = ax[0,2] ## 第1行第2列子区 ax3.margins(x=0.1,y=0.3) ax3.set_title("33333", fontsize =12) ax3.plot(x,y,ls="-",lw=2,color="g") ax4 = ax[1,0] ## 第2行第1列子区 ax4.margins(0.03) ax4.set_title("44444", fontsize =12) ax4.plot(x,y,ls="-",lw=2,color="b") # ax2 = fig.add_subplot(223) ax5 = ax[1,1] ## 第1行第2列子区 ax5.margins(0.7,0.7) ax5.set_title("55555", fontsize =12) ax5.plot(x,y,ls="-",lw=2,color="r") # ax3 = fig.add_subplot(224) ax6 = ax[1,2] ## 第2行第3列子区 ax6.margins(x=0.1,y=0.3) ax6.set_title("66666", fontsize =12) ax6.plot(x,y,ls="-",lw=2,color="g") fig.suptitle("main title",fontsize=25) plt.show()
-
一个子区一个子区添加
ax1 = plt.subplot(221)子区函数subplot(211)和子区函数subplot(212)代表首先在画布上分隔出一个2行1列的画布格式,然后在一个2行1列的画布格式上分别绘制图形1和图形2。
先确定当前在哪个画布上绘制,再设置坐标轴和绘制。
ax1 = plt.subplot(221) ax1.title() ax1.scatter() bx2 plt.subplot(222) ax2.title() ax2.plot() ax3 = plt.subplot(223) ax3.title() ax3.hist() ax4 = plt.subplot(224) ax4.title() ax4.pie()
合并等分画布的若干子区
ax1 = plt.add_subplot(121)
ax1 = plt.add_subplot(121) ## 1行2列布局的第1个子区的位置
ax1.margines(x=0.03, y=0.1) ## 曲线两侧空白的区域(以图线所占区间的比例表示,x=0.03表示x轴方向左右空白区域是0.03倍图形的横轴区间)
ax1.plot()
ax2 = plt.add_subplot(122) ## 2行2列布局的第2个子区的位置
ax2.plot()
ax3 = plt.add_subplot(124) ## 1行2列布局的第4个子区的位置
ax3.plot()

import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
x = np.linspace(0.0,2*np.pi)
y = np.cos(x)*np.sin(x)
ax1 = fig.add_subplot(211)
ax1.margins(0.03)
ax1.set_title("11111", fontsize =12) ## 子标题
ax1.plot(x,y,ls="-",lw=2,color="b")
ax2 = fig.add_subplot(223)
ax2.margins(0.7,0.7)
ax2.set_title("22222", fontsize =12)
ax2.plot(x,y,ls="-",lw=2,color="r")
ax3 = fig.add_subplot(224)
ax3.margins(x=0.1,y=0.3)
ax3.set_title("33333", fontsize =12)
ax3.plot(x,y,ls="-",lw=2,color="g")
fig.suptitle("main title",fontsize=25) ## 总标题
plt.show()

(2)跨越固定网格布局
将fig以网格划分,从而更精细地指定子区的位置和尺寸。
plt.subplot2grid((2,3),(1,0),rowspan=1, colspan=3)
(2,3)——将fig划分为2行3列
(1,0)——表示当前子区的在第2行第1列网格所在的子区(网格编号从0开始)
rowspan——子区所占的网格行数
colspan——子区所占的网格列数
import matplotlib as mpl
import matplotlib.pyplot as plt
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), colspan=1)
plt.title("空白绘图区域") ## 设置子区标题
# set subplot(23,4-6)
plt.subplot2grid((2,3),(1,0),colspan=3,rowspan=1)
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()


浙公网安备 33010602011771号