【问题标题】:Creating a correlation plot with matplotlib使用 matplotlib 创建相关图
【发布时间】:2014-11-17 22:08:19
【问题描述】:

我正在尝试在 python 中使用matplotlib 创建一个相关矩阵图。但是我无法使 x 和 y 刻度位于每个正方形的中间,而且我需要 xticks 是垂直的。我需要它们垂直,因为我将要绘制大约 15 个变量,因此不适合水平方式的刻度。

这是我的尝试:

import numpy as np
import matplotlib.pyplot as plt

X = np.random.randn(10,3)
X = X.T
R = np.corrcoef(X)
plt.figure()
plt.xticks([0,1,2], ["first", "second", "third"], rotation='vertical')
plt.yticks([0,1,2], ["first", "second", "third"])
plt.pcolor(R)

结果:

编辑:我刚刚发现使用 rotation='vertical' 可以解决我的 x-ticks 需求。

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    要集中刻度,您需要将 0.5 添加到每个值:

    plt.yticks([0.5,1.5,2.5], ["first", "second", "third"])
    plt.xticks([0.5,1.5,2.5], ["first", "second", "third"], rotation='vertical')
    

    此外,您可能需要添加以下内容,以便调整整体图形大小以考虑旋转的 x 标签:

    plt.tight_layout()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-26
      • 1970-01-01
      • 1970-01-01
      • 2017-10-22
      • 1970-01-01
      • 2013-08-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多