2014-09-29 23 views
2

我在python中使用matplotlib創建使用顏色條的協方差圖。但問題是ylabel繪製在彩條軸上。我無法找到如何解決這個問題。修復matplotlib中顏色條的ylabel位置

下面是一個例子:

enter image description here

這裏是我的代碼:

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") 

回答

3

更改爲

cb.ax.set_ylabel('Correlation Strength', rotation=270, labelpad=25) 

一般情況下,你應該期望的方便的功能做一個「最佳猜測「方法,並且您始終可以指定精確的軸,標籤等等,使用更通用的函數,在這種情況下可能是figtext。