Correlation Heatmap#

A heatmap showing pairwise correlation between numerical features in the penguins dataset. Darker colors represent stronger positive or negative correlations.

import hvplot.pandas  # noqa

df = hvplot.sampledata.penguins("pandas")
# Compute correlation matrix
corr = df[[c for c in df.columns if c.split("_")[-1] in ("mm", "g")]].corr()
# Convert to long-form for heatmap
corr_df = corr.stack().reset_index()
corr_df.columns = ['variable_1', 'variable_2', 'correlation']

corr_df.hvplot.heatmap(
    x='variable_1',
    y='variable_2',
    C='correlation',
    cmap='coolwarm',
    clim=(-1, 1),
    title='Correlation Heatmap (Bokeh)',
    hover_tooltips=[
        ("Variable 1", "@variable_1"),
        ("Variable 2", "@variable_2"),
        ("Correlation", "@correlation")
    ]
)
import hvplot.pandas  # noqa
hvplot.extension('matplotlib')

df = hvplot.sampledata.penguins("pandas")
# Compute correlation matrix
corr = df[[c for c in df.columns if c.split("_")[-1] in ("mm", "g")]].corr()
# Convert to long-form for heatmap
corr_df = corr.stack().reset_index()
corr_df.columns = ['variable_1', 'variable_2', 'correlation']

corr_df.hvplot.heatmap(
    x='variable_1',
    y='variable_2',
    C='correlation',
    cmap='coolwarm',
    clim=(-1, 1),
    title='Correlation Heatmap (Matplotlib)',
)
corr_df.head()
variable_1 variable_2 correlation
0 bill_length_mm bill_length_mm 1.000000
1 bill_length_mm bill_depth_mm -0.235053
2 bill_length_mm flipper_length_mm 0.656181
3 bill_length_mm body_mass_g 0.595110
4 bill_depth_mm bill_length_mm -0.235053
This web page was generated from a Jupyter notebook and not all interactivity will work on this website.