如何将`GridSpec()`与`subplots()`一起使用

Ins*_*Bot 23 python matplotlib python-2.7

有几个(例子)SO帖子涉及如何使用子GridSpec绘图.我试图实现无济于事,是允许使用GridSpecwith subplots,类似这样的东西,我可以用一些循环控制索引替换实际的数组和列表索引:

gs = gridspec.GridSpec(4, 1, height_ratios=[2, 2, 1, 1])        
tPlot, axes = plt.subplots(4, sharex=True, sharey=False)
tPlot.suptitle(node, fontsize=20)
axes[0].plot(targetDay[0], gs[0])
axes[1].plot(targetDay[1], gs[1])
axes[2].scatter(targetDay[2], gs[2])
axes[3].plot(targetDay[3], gs[3])
Run Code Online (Sandbox Code Playgroud)

不用说这段代码不起作用,它只是一个例子.

tmd*_*son 38

而不是调用gridspec.GridSpec之前subplots,您可以发送kwargsGridSpec您的内subplots通话,使用gridspec_kw参数.来自文档:

gridspec_kw :dict

带有关键字的Dict传递给GridSpec构造函数,用于创建子图所在的网格.

所以,例如:

import matplotlib.pyplot as plt

tPlot, axes = plt.subplots(
        nrows=4, ncols=1, sharex=True, sharey=False, 
        gridspec_kw={'height_ratios':[2,2,1,1]}
        )

tPlot.suptitle('node', fontsize=20)

axes[0].plot(range(10),'ro-') 
axes[1].plot(range(10),'bo-') 
axes[2].plot(range(10),'go-') 
axes[3].plot(range(10),'mo-') 

plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述