我在 matplotlib 中有一些我无法解释的奇怪行为,我想知道是否有人能看到发生了什么。本质上发生的是,我试图将过去的两个数字合二为一。我通过创建两个GridSpec对象来做到这一点,一个用于图形的左半边,另一个用于右半边。我绘制左侧并添加一个colorbar,但是当我在右侧选择我的第一个时,左侧的图形在颜色栏下方subplot向右移动。如果您尝试执行不包括最后两行的示例代码,您将看到您所期望的,但如果您执行全部代码,左边的图会发生变化。这是怎么回事?
import matplotlib.gridspec as gridspec
import numpy as np
import pylab as pl
scores = np.array([[ 0.32      ,  0.32      ,  0.32      ,  0.32      ,  0.32      ,
         0.32      ,  0.32      ,  0.32      ,  0.32      ],
       [ 0.32      ,  0.32      ,  0.32      ,  0.49333333,  0.85333333,
         0.92666667,  0.32      ,  0.32      ,  0.32      ],
       [ 0.32      ,  0.32      ,  0.51333333,  0.87333333,  0.96      ,
         0.95333333,  0.89333333,  0.44      ,  0.34      ],
       [ 0.32      ,  0.51333333,  0.88      ,  0.96      ,  0.96666667,
         0.95333333,  0.90666667,  0.47333333,  0.34      ],
       [ 0.51333333,  0.88      ,  0.96      ,  0.96      ,  0.96      ,
         0.96      ,  0.90666667,  0.47333333,  0.34      ],
       [ 0.88      ,  0.96      ,  0.96      ,  0.96      ,  0.94666667,
         0.96      ,  0.90666667,  0.47333333,  0.34      ],
       [ 0.96      ,  0.96      ,  0.96666667,  0.96      ,  0.94      ,
         0.96      ,  0.90666667,  0.47333333,  0.34      ],
       [ 0.96      ,  0.96666667,  0.96666667,  0.94666667,  0.94      ,
         0.96      ,  0.90666667,  0.47333333,  0.34      ],
       [ 0.96666667,  0.97333333,  0.96      ,  0.94666667,  0.94      ,
         0.96      ,  0.90666667,  0.47333333,  0.34      ],
       [ 0.96666667,  0.96666667,  0.96666667,  0.94666667,  0.94      ,
         0.96      ,  0.90666667,  0.47333333,  0.34      ],
       [ 0.95333333,  0.96      ,  0.96666667,  0.94666667,  0.94      ,
         0.96      ,  0.90666667,  0.47333333,  0.34      ]])
C_range = 10.0 ** np.arange(-2, 9)
gamma_range = 10.0 ** np.arange(-5, 4)
pl.figure(0, figsize=(16,6))
gs = gridspec.GridSpec(1,1)
gs.update(left=0.05, right=0.45, bottom=0.15, top=0.95)
pl.subplot(gs[0,0])
pl.imshow(scores, interpolation='nearest', cmap=pl.cm.spectral)
pl.xlabel('gamma')
pl.ylabel('C')
pl.colorbar()
pl.xticks(np.arange(len(gamma_range)), gamma_range, rotation=45)
pl.yticks(np.arange(len(C_range)), C_range)
gs = gridspec.GridSpec(3,3)
gs.update(left=0.5, right=0.95, bottom=0.05, top=0.95)
pl.subplot(gs[0,0])  # here's where the shift happens
