Skip to content

Instantly share code, notes, and snippets.

@eugen-hoppe
Last active February 20, 2025 09:36
Show Gist options
  • Select an option

  • Save eugen-hoppe/6009fc3bc061592dcd338d83371ec2a2 to your computer and use it in GitHub Desktop.

Select an option

Save eugen-hoppe/6009fc3bc061592dcd338d83371ec2a2 to your computer and use it in GitHub Desktop.
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import phik # pip install phik==0.12.4
# see: https://phik.readthedocs.io/en/latest/
def correlation_mtx(df_: pd.DataFrame, *columns: list[str], title: str = "") -> None:
# src: https://gist.github.com/eugen-hoppe/6009fc3bc061592dcd338d83371ec2a2
phik_data = pd.get_dummies(
df_[[*columns]],
drop_first=True,
)
phik_matrix = phik_data[phik_data.columns].phik_matrix(
interval_cols=phik_data.columns
)
plt.figure(figsize=(10, 8))
sns.heatmap(
phik_matrix, cmap="Blues", annot=True, fmt=".1f", annot_kws={"size": 6}
)
plt.title("Correlation Matrix" if not title else title, fontsize=14)
plt.xticks(fontsize=8)
plt.yticks(fontsize=8)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment