将ylabel位置固定在matplotlib中的颜色栏中

Jac*_*ain 2 python matplotlib

我在python中使用matplotlib来创建使用颜色条的协方差图.但问题是ylabel是在彩条轴上绘制的.我无法找到如何解决这个问题.

这是一个例子:

在此输入图像描述

这是我的代码:

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")
Run Code Online (Sandbox Code Playgroud)

mdu*_*ant 6

改成

cb.ax.set_ylabel('Correlation Strength', rotation=270, labelpad=25)
Run Code Online (Sandbox Code Playgroud)

一般来说,你应该期望方便函数做一个"最好的猜测"方法,你总是可以使用更一般的函数指定精确的轴,标签等位置,在这种情况下可能是figtext.