Displaying covariance

The covariance matrix explores the linear relationship between variables. For more information, see this demo as well. Here we will try to display the covariance as a matrix of shades, setting the value for each shade to observe the relative differences.

import matplotlib.pyplot as plt import numpy as np A = np.array([ [3,25], [4,20], [5,15], [6,10], [7, 5] ]) B = np.array([ [10,2], [12,5], [15,8], [19,11], [24,14] ]) C = np.array([ [2.1, 5], [2.2, 15], [2.3, 25], [2.2, 15], [1.1, 5] ]) for matrix in [A, B, C]: cov = np.cov(matrix) xdim, ydim = cov.shape plt.matshow(cov, cmap=plt.cm.gray) for x in range(xdim): for y in range(ydim): plt.annotate(cov[x,y], xy=(x,y), xytext=(x-0.35, y+0.1), \ color='#13ffd3', fontsize=14, fontweight='bold') plt.axis('off') plt.show()

This leads to the following covariance matrices for A, B and C with lighter boxes indicating a stronger relationship.

Covariance matrix ACovariance matrix BCovariance matrix C