Python Matplotlib中不小心移动绘图
我在matplotlib中有一些奇怪的行为,我无法解释,我想知道是否有人能看到发生了什么。本质上发生的是,我试图把过去的两个数字合并成一个。为此,我创建了两个Python Matplotlib中不小心移动绘图,python,matplotlib,Python,Matplotlib,我在matplotlib中有一些奇怪的行为,我无法解释,我想知道是否有人能看到发生了什么。本质上发生的是,我试图把过去的两个数字合并成一个。为此,我创建了两个GridSpec对象,一个用于图形的左半部分,另一个用于右半部分。我画左手边并添加一个色条,但当我在右手边选择我的第一个子地块时,左边的图形在色条下向右移动。如果您尝试执行不包括最后两行的示例代码,您将看到您期望的结果,但是如果您执行全部代码,则左侧的绘图将移动。发生什么事了 import matplotlib.gridspec as gr
GridSpec
对象,一个用于图形的左半部分,另一个用于右半部分。我画左手边并添加一个色条
,但当我在右手边选择我的第一个子地块
时,左边的图形在色条下向右移动。如果您尝试执行不包括最后两行的示例代码,您将看到您期望的结果,但是如果您执行全部代码,则左侧的绘图将移动。发生什么事了
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
您可以在#这里是移位发生的地方之后创建颜色栏
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)
ax = pl.subplot(gs[0,0]) # save the axes to ax
pl.imshow(scores, interpolation='nearest', cmap=pl.cm.spectral)
pl.xlabel('gamma')
pl.ylabel('C')
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
pl.colorbar(ax=ax) # create colorbar for ax
pl.show()