Last active
September 22, 2021 17:37
-
-
Save WillKoehrsen/4a07729aa3b74d018069e36e05b6bb1d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Function to calculate correlation coefficient between two arrays | |
def corr(x, y, **kwargs): | |
# Calculate the value | |
coef = np.corrcoef(x, y)[0][1] | |
# Make the label | |
label = r'$\rho$ = ' + str(round(coef, 2)) | |
# Add the label to the plot | |
ax = plt.gca() | |
ax.annotate(label, xy = (0.2, 0.95), size = 20, xycoords = ax.transAxes) | |
# Create a pair grid instance | |
grid = sns.PairGrid(data= df[df['year'] == 2007], | |
vars = ['life_exp', 'log_pop', 'log_gdp_per_cap'], size = 4) | |
# Map the plots to the locations | |
grid = grid.map_upper(plt.scatter, color = 'darkred') | |
grid = grid.map_upper(corr) | |
grid = grid.map_lower(sns.kdeplot, cmap = 'Reds') | |
grid = grid.map_diag(plt.hist, bins = 10, edgecolor = 'k', color = 'darkred'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment