Gridspec范围错误

时间:2015-10-19 18:38:31

标签: python python-2.7 matplotlib plot

我对matplotlib的gridspec有一个简单的错误,我似乎无法弄明白。有人能告诉我哪里出错了吗?

import matplotlib.pyplot as plot
import matplitlib.gridspec as gridspec
gs = gridspec.GridSpec(1,3, width_ratios = [1,1], height_ratios = [2,1])
fig = plot.figure(figsize=(20,10))
ax1 = plot.subplot(gs[:, :-1])
ax2 = plot.subplot(gs[:, -1])

我使用此代码获得的错误是

IndexError: index 4 is out of bounds for axis 0 with size 4

这对我没有意义。我认为我用这段代码说的是第一轴(ax1)应该占用所有行,并且存在于前两列中。第二个轴(ax2)应该占用所有行,并且仅存在于第三列中。这不是我的代码实际意味着什么吗?

1 个答案:

答案 0 :(得分:4)

gs = gridspec.GridSpec(1,3)表示有1行和3列,但是 width_ratios = [1,1]表示有2列,height_ratios = [2,1]表示有2列。不幸的是,matplotlib没有发现gs实例化时的矛盾,但是矛盾导致后来的错误

ax1 = plot.subplot(gs[:, :-1])

被调用。要修复错误,您可以指定3个宽度比和单个高度比:

gs = gridspec.GridSpec(1,3, width_ratios=[1,2,3], height_ratios=[1])

例如。

相关问题