2015-04-02 89 views
7

我想用Python 3.4(示例here)中的matplotlib編寫一個函數來生成Matlab風格關聯圖。但是,我想要更改圖,以便對角線子圖顯示變量的名稱,下三角子圖顯示皮爾遜相關係數,而上三角子圖顯示散點圖。下面是一些代碼來生成示例數據和我寫的函數。它會在正確的位置顯示具有變量名稱和相關係數的相應4x4子圖的網格,但散點圖不顯示。當使用for循環填充時,matplotlib散點圖不顯示

import numpy as np 
import matplotlib.pyplot as plt 

means = [0, 1, 0, 2] 
sig = [[1, 0.5, 0, -0.1], [0.5, 3, 0, 0.2], [0, -0.1, 1, -0.3], [-0.1, 0.2, -0.3, 1]] 
data = np.random.multivariate_normal(means, sig, 50) 
names = ['Var' + str(i) for i in range(data.shape[1])] 

def corrplot(data, names): 
    corrMat = np.corrcoef(data, rowvar = 0) 
    numVars = data.shape[1] 

    fig, ax = plt.subplots(numVars, numVars, sharex = "col", sharey = "row") 
    fig.subplots_adjust(wspace = 0, hspace = 0) 

    for i in range(numVars): 
     for j in range(numVars): 
      if i == j: # On the diagonal 
       ax[i, j].text(0.5, 0.5, names[i], transform = ax[i, j].transAxes) 
      elif i < j: # In the upper triangle 
       ax[i, j].scatter(data[:, i], data[:, j], marker = '.') 
      elif i > j: # In the lower triangle 
       ax[i, j].text(0.5, 0.5, str(round(corrMat[i, j], 3)), transform = ax[i, j].transAxes) 
    plt.show() 

在試圖確定問題的來源,我手動重構的情節爲使用以下代碼,從而產生所希望的圖的2變量的情況下:

fig, ax = plt.subplots(2, 2, sharex = "col", sharey = "row") 
fig.subplots_adjust(wspace = 0, hspace = 0) 
ax[0, 0].text(0.5, 0.5, 'Var0', transform = ax[0, 0].transAxes) 
ax[0, 1].scatter(data[:, i], data[:, j], marker = '.') 
ax[1, 0].text(0.5, 0.5, '0.5', transform = ax[1, 0].transAxes) 
ax[1, 1].text(0.5, 0.5, 'Var1', transform = ax[1, 1].transAxes) 
plt.show() 

由於其工作原理,我假設這個問題與混合文本和數據在子圖中無關。我編寫了下一個函數來測試使用for循環來填充子圖,並按照預期在每個子圖中生成散點圖。

def test1(data): 
    numVars = data.shape[1] 
    fig, ax = plt.subplots(numVars, numVars, sharex = "col", sharey = "row") 
    fig.subplots_adjust(wspace = 0, hspace = 0) 

    for i in range(numVars): 
     for j in range(numVars): 
      ax[i, j].scatter(data[:, i], data[:, j], marker = '.') 
    plt.show() 

接下來,我嘗試僅使用for循環填充子圖的子集。這會產生一個空白網格,如下所示。

def test2(data): 
    numVars = data.shape[1] 
    fig, ax = plt.subplots(numVars, numVars, sharex = "col", sharey = "row") 
    fig.subplots_adjust(wspace = 0, hspace = 0) 

    for i in range(numVars): 
     for j in range(i + 1, numVars): 
      ax[i, j].scatter(data[:, i], data[:, j], marker = '.') 
    plt.show() 

這使我相信,有相關的for循環以及如何創建散點圖一些錯誤,但我一直沒能找到錯誤呢。

+0

喜替換它並歡迎這樣的!創建一個MCVE的榮譽,非常感謝。它不是100%的函數,但是,你需要將名稱的定義更改爲'names = ['var'+ str(i)for i in range(data.shape [1])]'(string to'str'你忘了'範圍')。一旦我這樣做,並添加了一個電話'corrplot',我實際上得到的結果工作得很好。你使用的是什麼版本的matplotlib? – Ajean 2015-04-02 15:09:31

+0

我正在使用matplotlib 1.4.3。我使用Anaconda發行版,並在昨天更新了一切,以確保舊版本不是問題。 – 2015-04-02 15:13:32

+0

您對默認的rcParams有任何更改嗎?這是我唯一能想到的其他事情,可能會使這些點消失。試着把'plt.rcdefaults()'放在頂部,看看你得到了什麼。 – Ajean 2015-04-02 15:17:43

回答

1

您的代碼完全顯示所需的圖。 我覺得你的matplolib版本不承認marker = '.'

你可以嘗試用默認的標記,以圖(無marker = '.')或marker = 'o'

+0

最終我找到了一個不使用matplotlib的解決方案。然而,用更新版本的matplotlib重新訪問它,它可以很好地工作。感謝你的回答;這讓我重新考慮了這一點,並意識到它確實有效。 – 2016-05-02 18:23:16