python - 在 matplotlib 的颜色条中修复 ylabel 位置

标签 python matplotlib

我在 python 中使用 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")

最佳答案

改为

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

一般来说,您应该期望便利函数执行“最佳猜测”方法,并且您始终可以使用更通用的函数指定精确的轴、标签等、位置,在本例中可能是 figtext。

关于python - 在 matplotlib 的颜色条中修复 ylabel 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26101262/

相关文章:

python - 如何在 python 中自动最大化 pylab 绘图窗口?

python - Python图中的上标

python - 使用 matplotlib *没有* TCL

python - 子图的不同行高

在命令行上运行Python脚本找不到站点包

python - 在 Python 中如何将 `email.message.Message` 对象转换为 `email.message.EmailMessage` 对象

python - Scipy LeastSq 误差线

python - 使用 win32COM 时创建新的 Python 对象

python - SQLAlchemy 如何为同一个表定义两个模型

python - Matplotlib 不听字体选择