A correlation matrix is the one table every analyst builds on day one: for every pair of numeric columns, how strongly do they move together? It is also hard to read as a grid of numbers once there are more than five columns. A heatmap turns each number into a colour, so strong relationships jump out. But a heatmap can mislead as easily as it can reveal: if the colour scale is not pinned to the same range as the data, a correlation of 0.2 can look as strong as 0.9, and a matrix drawn in full shows every pair twice, since correlation is symmetric.
This question builds the whole path: compute the matrix, draw it with a scale that always runs from -1 to 1 and is centred on 0, hide the redundant half with a mask, and write the numbers in the cells so the colours can be checked.
Implement correlation_matrix(df), heatmap_axes(corr, annotate), upper_triangle_mask(n) and masked_heatmap(corr). The signatures and docstrings are already in the editor.
correlation_matrix(df) returns the Pearson correlation matrix of the numeric columns of df (text and boolean columns are left out) as a DataFrame with the column names as both index and columns.heatmap_axes(corr, annotate) draws corr with sns.heatmap: colour map "coolwarm", the colour scale fixed to run from -1 to 1 (vmin=-1, vmax=1) and centred at 0, square cells. If annotate is true every cell shows its value formatted with two decimals (".2f"). Returns the Axes.upper_triangle_mask(n) returns an n × n boolean NumPy array that is True strictly above the diagonal and False on and below it.masked_heatmap(corr) is heatmap_axes(corr, annotate=True) with the cells above the diagonal hidden by that mask (so only the diagonal and the lower triangle are drawn and annotated). Returns the Axes.plt.show().df.select_dtypes("number").corr() computes the matrix for the numeric columns.
sns.heatmap takes vmin, vmax, center, cmap, annot, fmt, square and a mask where True means 'do not draw this cell'.
np.triu(np.ones((n, n), dtype=bool), k=1) is the triangle above the diagonal.
No results yet
Run your code to check it against the examples, or submit to run every test.