2015-12-14 46 views
16

有几个(example)SO帖子处理如何使用GridSpec一般subplotting。 什么,我试图实现无果,是允许使用的GridSpecsubplots,这样的事情,在那里我可以与一些循环控制指标代替实际的数组和列表索引:如何使用`GridSpec()`与`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]) 

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

回答

25

不是subplots收到主叫gridspec.GridSpec,您可以从subplots调用内发送kwargsGridSpec,使用gridspec_kw说法。从docs

gridspec_kw:快译通

快译通用传递给用于创建次要情节被放置在网格中的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() 

enter image description here

相关问题