Skip to content

Instantly share code, notes, and snippets.

@dyerrington
Last active January 27, 2022 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dyerrington/1f7b7d9bf0d48087df5d333ff3b69e5e to your computer and use it in GitHub Desktop.
Save dyerrington/1f7b7d9bf0d48087df5d333ff3b69e5e to your computer and use it in GitHub Desktop.
Nicer function to plot Pearson scores using Pandas and seaborn. Updated in 2022 for better aesthetics.
def daves_pearson(corr, threashold = False, empty_dimensions = True, title = False):
"""
Based on http://seaborn.pydata.org/examples/many_pairwise_correlations.html
Parameters
-----------------------
corr : Pandas Pearson coorelation matrix object
threashold : Threashold filter for absolute score value. Useful to suppress display of
non-correlated values.
empty_dimensions : Removes features that don't meet threashold.
title : Plot title.
"""
if threashold:
corr = corr[corr.abs() >= threashold]
if not empty_dimensions:
# drop dimensions with nothing in them after threashold filter
# and after removing matrix identity
np.fill_diagonal(corr.values, np.NAN)
corr = corr.dropna(how = "all", axis = 0) # columns
corr = corr.dropna(how = "all", axis = 1) # rows
# Generate a mask for the upper triangle
mask = np.zeros_like(corr, dtype=bool)
mask[np.triu_indices_from(mask)] = True
# Set up the matplotlib figure
f, ax = plt.subplots(figsize=(11, 9))
# Generate a custom diverging colormap
cmap = sns.color_palette("vlag", as_cmap=True)
# Draw the heatmap with the mask and correct aspect ratio
sns.heatmap(corr, mask=mask, vmax=.3, annot=True, cmap = cmap,
square = True,
linewidths = 1, linecolor='lightgrey', cbar_kws={"shrink": .5}, ax=ax)
ax.axhline(y=0, color='k',linewidth=1)
ax.axhline(y=corr.shape[1], color = 'k',linewidth=3)
ax.axvline(x=0, color='k',linewidth = 1)
ax.axvline(x=corr.shape[0], color = 'k',linewidth=3)
if title:
ax.set_title(title)
plt.xticks(rotation=45)
plt.yticks(rotation=0)
daves_pearson(corr_all, threashold = .2, empty_dimensions = False, title = "Test")
@dyerrington
Copy link
Author

You can add the cmap back in if you like.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment