pythonpandasplotcovariance-matrix

Plot existing covariance dataframe


I have computed a covariance of 26 inputs from another software. I have an existing table of the results. See image below: enter image description here

What I want to do is enter the table as a pandas dataframe and plot the matrix. I have seen the thread here: Plot correlation matrix using pandas. However, the aforementioned example, computed the covariance first and plotted the 'covariance' object. In my case, I want to plot the dataframe object to look like the covariance matrix in the example.

Link to data: HERE.


Solution

  • IIUC, you can use seaborn.heatmap with annot=True :

    plt.figure(figsize=(6, 4))
    
    (
        pd.read_excel("/tmp/Covariance Matrix.xlsx", header=None)
            .pipe(lambda df: sns.heatmap(df.sample(10).sample(10, axis=1), annot=True, fmt=".1f"))
    );
    
    # for a sample of 10 rows / 10 columns
    

    Output :

    enter image description here

    And, as suggested by stukituk in the comments, you can add cmap="coolwarm" for colors :

    enter image description here