【4.2】matplotlib-多图合并-subplot/subplots/subplot2grid
一、subplot2grid
ax = plt.subplot(2, 2, 1)
##等价于
ax = plt.subplot2grid((2, 2), (0, 0))
subplot2grid 索引是从0开始的。
跨行或者跨列,可以这样:
ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=2)
ax3 = plt.subplot2grid((3, 3), (1, 2), rowspan=2)
例子:
import matplotlib.pyplot as plt
ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3)
ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=2)
ax3 = plt.subplot2grid((3, 3), (1, 2), rowspan=2)
ax4 = plt.subplot2grid((3, 3), (2, 0))
ax5 = plt.subplot2grid((3, 3), (2, 1))
plt.show()
二、GridSpec and SubplotSpec
ax = plt.subplot2grid((2, 2), (0, 0))
等价于
import matplotlib.gridspec as gridspec
gs = gridspec.GridSpec(2, 2)
ax = plt.subplot(gs[0, 0])
例子:
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
gs = gridspec.GridSpec(3, 3)
ax1 = plt.subplot(gs[0, :])
ax2 = plt.subplot(gs[1, :-1])
ax3 = plt.subplot(gs[1:, -1])
ax4 = plt.subplot(gs[-1, 0])
ax5 = plt.subplot(gs[-1, -2])
plt.show()
调整布局:
gs1 = gridspec.GridSpec(3, 3)
gs1.update(left=0.05, right=0.48, wspace=0.05)
例子:
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
gs1 = gridspec.GridSpec(3, 3)
gs1.update(left=0.05, right=0.48, wspace=0.05)
ax1 = plt.subplot(gs1[:-1, :])
ax2 = plt.subplot(gs1[-1, :-1])
ax3 = plt.subplot(gs1[-1, -1])
gs2 = gridspec.GridSpec(3, 3)
gs2.update(left=0.55, right=0.98, hspace=0.05)
ax4 = plt.subplot(gs2[:, :-1])
ax5 = plt.subplot(gs2[:-1, -1])
ax6 = plt.subplot(gs2[-1, -1])
plt.show()
gs0 = gridspec.GridSpec(1, 2)
gs00 = gridspec.GridSpecFromSubplotSpec(3, 3, subplot_spec=gs0[0])
gs01 = gridspec.GridSpecFromSubplotSpec(3, 3, subplot_spec=gs0[1])
三、GridSpec with Varying Cell Sizes
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
gs = gridspec.GridSpec(2, 2,
width_ratios=[1, 2],
height_ratios=[4, 1]
)
ax1 = plt.subplot(gs[0])
ax2 = plt.subplot(gs[1])
ax3 = plt.subplot(gs[2])
ax4 = plt.subplot(gs[3])
plt.show()
四、subplots
创建数据
>>> x = np.linspace(0, 2*np.pi, 400)
>>> y = np.sin(x**2)
一个subplot
>>> fig, ax = plt.subplots()
>>> ax.plot(x, y)
>>> ax.set_title('Simple plot')
创建两个subplot
>>> f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
>>> ax1.plot(x, y)
>>> ax1.set_title('Sharing Y axis')
>>> ax2.scatter(x, y)
Creates four polar axes, and accesses them through the returned array
>>> fig, axes = plt.subplots(2, 2, subplot_kw=dict(polar=True))
>>> axes[0, 0].plot(x, y)
>>> axes[1, 1].scatter(x, y)
Share a X axis with each column of subplots
>>> plt.subplots(2, 2, sharex='col')
Share a Y axis with each row of subplots
>>> plt.subplots(2, 2, sharey='row')
Share both X and Y axes with all subplots
>>> plt.subplots(2, 2, sharex='all', sharey='all')
Note that this is the same as
>>> plt.subplots(2, 2, sharex=True, sharey=True)
四、讨论
4.1 调节子图之间的距离
from matplotlib.pyplot import subplots_adjust
subplots_adjust(left=0.15,bottom=0.1,top=0.9,right=0.95,hspace=0.2,wspace=0.25)
可以看到这四个参数用来控制上下左右的空白,注意这里面是从figure的左下角开始标记,取值从0-1。top和right的取值也是以左下角为坐标原点计数,并不是表示到最上方和最右边的比例。比如top=0.9表示给上面留下0.1的空白
wspace和hspace分别控制子图之间的列距和行距
4.2 控制 matplotlib 子图大小
import numpy as np
import matplotlib.pyplot as plt
'''调整 matplotlib 子图的大小'''
x1 = np.linspace(0.0, 5.0)
x2 = np.linspace(0.0, 2.0)
y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
y2 = np.cos(2 * np.pi * x2)
plt.figure()
rect1 = [0.14, 0.35, 0.77, 0.6] # [左, 下, 宽, 高] 规定的矩形区域 (全部是0~1之间的数,表示比例)
rect2 = [0.14, 0.05, 0.77, 0.2]
ax1 = plt.axes(rect1)
ax2 = plt.axes(rect2)
ax1.plot(x1, y1, '-og', ms=3)
ax2.plot(x2, y2, '-ob', ms=3)
plt.show()
或者这样:
import numpy as np
import matplotlib.pyplot as plt
x1 = np.linspace(0.0, 5.0)
x2 = np.linspace(0.0, 2.0)
y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
y2 = np.cos(2 * np.pi * x2)
plt.axes([0.14, 0.35, 0.77, 0.6])
plt.plot(x1, y1, 'yo-')
plt.axes([0.14, 0.05, 0.77, 0.2])
plt.plot(x2, y2, 'r.-')
plt.show()
注:
可以加入这一样来调节图片的大小
plt.figure(figsize=(70,10))
参考资料
这里是一个广告位,,感兴趣的都可以发邮件聊聊:tiehan@sina.cn
个人公众号,比较懒,很少更新,可以在上面提问题,如果回复不及时,可发邮件给我: tiehan@sina.cn
个人公众号,比较懒,很少更新,可以在上面提问题,如果回复不及时,可发邮件给我: tiehan@sina.cn