Python 修复matplotlib中颜色栏中的ylabel位置
我正在使用python中的matplotlib使用颜色栏创建协方差图。但问题是,ylabel是在颜色条轴上绘制的。我找不到解决这个问题的方法 下面是一个例子: 这是我的密码:Python 修复matplotlib中颜色栏中的ylabel位置,python,matplotlib,Python,Matplotlib,我正在使用python中的matplotlib使用颜色栏创建协方差图。但问题是,ylabel是在颜色条轴上绘制的。我找不到解决这个问题的方法 下面是一个例子: 这是我的密码: def create_covariance_plot(X, name): """ Visualizes the covariance matrix for a design matrix. Parameters: ----------- X:a design matrix whe
def create_covariance_plot(X, name):
"""
Visualizes the covariance matrix for a design matrix.
Parameters:
-----------
X:a design matrix where each column is a feature and each row is an observation.
name: the name of the plot.
"""
pylab.figure()
n_cols = X.shape[1]
X = X.T
R = np.corrcoef(X)
pylab.pcolor(R)
cb = pylab.colorbar()
cb.ax.set_ylabel('Correlation Strength', rotation=270)
pylab.yticks(np.arange(0, n_cols + 0.5),range(0,n_cols))
pylab.xticks(np.arange(0, n_cols + 0.5),range(0,n_cols))
pylab.xlim(0, n_cols)
pylab.ylim(0, n_cols)
pylab.xlabel("Feature")
pylab.ylabel("Feature")
pylab.savefig(name + ".png")
改为
cb.ax.set_ylabel('Correlation Strength', rotation=270, labelpad=25)
通常,您应该期望便利函数执行“最佳猜测”方法,并且始终可以使用更通用的函数(在本例中可能是figtext)指定精确的轴、标签等位置