image_output_1.png
import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import seaborn as sns

data = pd.read_csv('../input/world-happiness/2019.csv')

correlation_matrix_data = data.corr()

# Correlation Matrix function in Matplotlib 👩‍🚒
fig, ax= plt.subplots(figsize=(6,6))
cp=ax.matshow(correlation_matrix_data)
ax.set_title('Correlation Matrix Plot')
# Annotate the values into correlation plot
for (i, j), z in np.ndenumerate(correlation_matrix_data):
    ax.text(j, i, '{:0.2f}'.format(z), ha='center', va='center')
fig.colorbar(cp, ax=ax,fraction=0.046, pad=0.04);

# fraction=0.046, pad=0.04 seem like a magical combination to keep the colorbar scaled to the plot all the time 🙄

-Seaborn
-sns.heatmap(df.corr())

# Correlation Matrix function in Seaborn 👨‍🚀
fig, ax= plt.subplots(figsize=(8,8))
cmap = plt.get_cmap('magma',30)
ax= sns.heatmap(correlation_matrix_data, annot=True, fmt="0.3f", square=True
               ,cmap=cmap, cbar_kws={"shrink": 0.8})
ax.set_title('Correlation Matrix Plot', fontsize=13);

# annot=True: annotate values into the corr matrix
# cbar_kws={"shrink": 0.8} : change the size of the colorbar to 0.8 of the plot 🙆‍♀️

Group = np.repeat(np.array(['Q1', 'Q2', 'Q3']), [52, 52, 52], axis=0)
Group = pd.Series(Group)
pair_plot_data = data[['Social support', 'Healthy life expectancy', 'Score', 'GDP per capita']]
pair_plot_data['Group'] = Group
pair_plot_data.iloc[[0, 52, 104]]

grid = sns.PairGrid(data=pair_plot_data, size=2.5, hue='Group')
grid = grid.map_upper(plt.scatter, linewidths=0.3, edgecolor='black', alpha=0.7)
grid = grid.map_diag(sns.kdeplot, fill=True, bw=0.3)
grid = grid.map_lower(plt.hist2d, bins=30, lw=0.0)

histogram_data = data[['Score', 'GDP per capita']]

sns.set(rc={'figure.figsize': (10, 4)})
sns.set_theme(style='whitegrid')
ax = sns.histplot(x='Score', data=histogram_data, bins=30, color='darkgreen', edgecolor='black', kde=True)
ax = sns.histplot(x='GDP per capita', data=histogram_data, bins=10, color='darkblue', edgecolor='black', kde=True)