Skip to content

Instantly share code, notes, and snippets.

@eric-czech
Last active December 2, 2024 12:59
Show Gist options
  • Save eric-czech/741c9c088a04f8c83216ecc7e52e991d to your computer and use it in GitHub Desktop.
Save eric-czech/741c9c088a04f8c83216ecc7e52e991d to your computer and use it in GitHub Desktop.
Clustered heatmap example
def get_clustered_order(df: pd.DataFrame, metric: str='euclidean', method: str='average') -> pd.DataFrame:
from scipy.cluster.hierarchy import linkage, leaves_list
return leaves_list(linkage(df, metric=metric, method=method))
def get_clustered_dataframe(df: pd.DataFrame, fill_value: Any=None, **kwargs) -> pd.DataFrame:
dfs = df if fill_value is None else df.fillna(fill_value)
return df.iloc[get_clustered_order(dfs, **kwargs), get_clustered_order(dfs.T, **kwargs)]
import plotly.express as px
px.imshow(get_clustered_dataframe(df))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment