我在 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")
最佳答案
改为
cb.ax.set_ylabel('Correlation Strength', rotation=270, labelpad=25)
一般来说,您应该期望便利函数执行“最佳猜测”方法,并且您始终可以使用更通用的函数指定精确的轴、标签等、位置,在本例中可能是 figtext。
关于python - 在 matplotlib 的颜色条中修复 ylabel 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26101262/