Featured resource
Tech Upskilling Playbook 2025
Tech Upskilling Playbook

Build future-ready tech teams and hit key business milestones with seven proven plays from industry leaders.

Learn more
  • Labs icon Lab
  • Data
Labs

Build a Heatmap in Python to Visualize Correlations between Ad Spend and CTR

In this Code Lab, Visualize A/B Test Performance with Seaborn Heatmaps, you'll learn to transform marketing test data into clean, annotated heatmaps that communicate key relationships. You'll compute a correlation matrix, build and style heatmaps using Matplotlib and Seaborn, and apply presentation polish to produce professional-quality visuals. By the end, you’ll know how to clearly show the strength of relationships—like how Ad Spend and CTR correlate—in a way that’s ready for stakeholder reports.

Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 58m
Last updated
Clock icon Aug 18, 2025

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    Step 1: Generate a Correlation Matrix

    Step 1: Generate a Correlation Matrix

    Understanding relationships between variables is a key first step in data exploration. In this step, you’ll create a correlation matrix to see how numeric variables in your marketing dataset relate to each other.

    The correlation matrix shows Pearson correlation coefficients for every pair of numeric columns. Values range from -1 to +1:

    • +1 indicates a perfect positive linear relationship.
    • 0 indicates no linear relationship.
    • –1 indicates a perfect negative linear relationship.

    What You’ll Learn in This Step

    • Compute a correlation matrix with df.corr()
    • Access a specific pair of correlations (e.g., Ad Spend vs. CTR)
    • Interpret correlation coefficients in the context of marketing data

    Open the Notebook

    From the workspace panel, open the notebook file: 1-step-one.ipynb.

    info> Important: You must save your notebook (Ctrl/Cmd + S) before clicking Validate. Validation checks the most recent saved checkpoint.

    How to Complete Each Task
    • Find the matching code cell labeled Task 1.1, Task 1.2, etc.
    • Follow the # TODO comments and write your code.
    • Run the cell using the Run button or by pressing Shift + Enter.
    • Save your progress before validation.
    • All code and output will appear inline in the notebook.
    ### Computing a Correlation Matrix with Pandas

    A correlation matrix helps you quickly see how all numeric columns in your dataset relate to each other.


    How It Works

    • The .corr() method computes pairwise Pearson correlation coefficients between every numeric column.
    • Each cell in the resulting DataFrame shows the correlation between two variables.
    • The diagonal values will always be 1.0 because each column is perfectly correlated with itself.

    Syntax Example
    correlation_matrix = dataframe.corr()
    

    Interpreting Correlations
    • Near +1: Strong positive relationship (as one variable increases, so does the other).
    • Around 0: No linear relationship.
    • Near –1: Strong negative relationship (as one variable increases, the other decreases).

    Why This Is Used for Heatmaps

    Heatmaps visually encode the correlation matrix by coloring each cell according to its numeric value. This makes it easy to spot:

    • Strong positive or negative relationships between pairs of variables.
    • Patterns and clusters across many features at once.
    • The overall structure of correlations without reading every number manually.

    This is why computing the correlation matrix is the first step before creating a heatmap. ### Retrieving a Specific Correlation Value

    After creating the correlation matrix, you often want to isolate the correlation between two specific columns.


    How It Works

    • The correlation matrix is a DataFrame where rows and columns are labeled by column names.
    • You can access the correlation between any two variables by using .loc[row, column].
    • This returns a single numeric value representing their relationship strength.

    Example
    correlation_value = corr_matrix.loc["columnA", "columnB"]
    

    When to Use This
    • To highlight or report a key relationship (e.g., between Ad Spend and CTR).
    • To confirm the exact numeric correlation before creating a visualization.
    • To validate that the correlation direction and magnitude match expectations. #### Rounding the Correlation Matrix

    When you print a correlation matrix, the default output shows many decimal places. Rounding helps you quickly see the most important values without extra noise.


    How to Round a DataFrame

    • Use .round(decimals) to set the number of decimal places.
    • This returns a new DataFrame with rounded values.

    Example
    rounded_matrix = corr_matrix.round(2)
    

    When to Round

    • Before printing in reports or console.
    • When you want a clean summary of correlation strengths.
    • To make heatmap labels simpler. #### Sorting Correlation Values

    When you want to find which variables are most strongly related to a target (like Ad Spend), it helps to sort the correlations in order.


    How to Extract and Sort

    • Use .loc[row] to get correlations between one column and all others.
    • Call .sort_values() to order them ascending or descending.

    Example
    correlations = corr_matrix.loc["columnName"]
    sorted_corrs = correlations.sort_values(ascending=False)
    

    Why This Matters

    • You quickly see which variables are most strongly linked.
    • Helps prioritize which relationships to explore visually.
  2. Challenge

    Step 2: Plot a Basic Heatmap with Matplotlib

    Step 2: Plot a Basic Heatmap with Matplotlib

    Heatmaps provide a visual summary of how variables correlate across a dataset. Before moving on to Seaborn or advanced formatting, it’s important to understand how Matplotlib alone renders these grids.

    In this step, you’ll use Matplotlib’s imshow() to plot the raw correlation matrix, add labels and a colorbar, and adjust the figure size for readability.


    What You’ll Learn in This Step

    • Use plt.imshow() to render a matrix as a grid of colors
    • Label the rows and columns of the matrix
    • Add a colorbar to show correlation scale
    • Adjust figure size and aspect ratio to improve readability

    Open the Notebook

    From the workspace panel, open the notebook file: 2-step-two.ipynb.

    info> Important: You must save your notebook (Ctrl/Cmd + S) before clicking Validate. Validation checks the most recent saved checkpoint.

    How to Complete Each Task
    • Find the matching code cell labeled Task 2.1, Task 2.2, etc.
    • Fill in the blanks and follow the # TODO comments in each cell.
    • Run the cell using the Run button or by pressing Shift+Enter.
    • Save your progress using the Save icon or File > Save and Checkpoint.
    • All code and output will appear inline in the notebook.
    ### Plotting a Matrix with Matplotlib `imshow()`

    Matplotlib’s imshow() function can display a 2D array as a grid of colored cells. This is the most basic way to create a heatmap in Python.


    How imshow() Works

    • It takes a 2D array (like a correlation matrix) and maps each value to a color.
    • The color scale uses a default colormap (usually “viridis”).
    • By default, there are no axis labels or colorbar.

    Basic Example

    plt.imshow(matrix)
    

    This displays the array as an image.


    Why Start with Raw imshow()?

    • It shows the minimum code needed to render the matrix.
    • It helps you see how raw Matplotlib behaves before customizing.
    • It builds intuition about how correlation values become colors. ### Adding Labels and a Colorbar to Your Heatmap

    While imshow() displays the data visually, the default chart has no axis labels or scale legend, which makes it difficult to interpret. To create a clear and useful heatmap, you need to:

    • Label the x-axis and y-axis with the column names from your DataFrame.
    • Add a colorbar to show the numeric meaning of the colors.

    Axis Tick Labels

    Matplotlib requires you to specify:

    • The positions where labels appear.
    • The labels themselves.

    You do this with plt.xticks() and plt.yticks().


    How It Works

    • range(len(corr_matrix)) creates a sequence of positions (0, 1, 2, ...).
    • corr_matrix.columns provides the labels for each column.

    Example: Labeling the Ticks Inline

    This is the most direct way to label axes:

    plt.xticks(range(len(corr_matrix)), corr_matrix.columns, rotation=45)
    plt.yticks(range(len(corr_matrix)), corr_matrix.columns)
    

    Tip: Rotating the x-axis labels helps avoid overlap if column names are long.


    Advanced: Using Variables for Positions and Labels

    Instead of passing range() and columns directly, you can store them in variables first. This makes your code easier to reuse or debug:

    positions = range(len(corr_matrix))
    labels = corr_matrix.columns
    
    plt.xticks(positions, labels, rotation=45)
    plt.yticks(positions, labels)
    

    This approach is helpful when:

    • You want to reuse positions or labels later.
    • You have more complex logic for labeling.
    • You want to keep your plt.xticks() calls shorter.


    Colorbar

    The colorbar shows how colors map to numeric correlation values.

    Add it with:

    plt.colorbar()
    

    This automatically scales from the minimum to maximum correlation values in your matrix.


    ### Adjusting Figure Size and Using Subplots

    After you’ve labeled your heatmap, the next step is to improve readability by:

    • Making the figure bigger.
    • Avoiding squished labels.
    • Using the subplots() approach, which gives you more control over the figure and axes.

    Why Use plt.subplots()?

    Instead of plt.figure(), plt.subplots():

    Creates both a Figure (fig) and an Axes (ax) object. Makes it easier to customize things like:

    • Size (figsize)
    • Axis labels (ax.set_xticks(), etc.)
    • Colorbars (fig.colorbar())

    This is a more scalable pattern for professional code.


    Example: Creating a Figure and Axes
    fig, ax = plt.subplots(figsize=(8, 6))
    
    • figsize=(8, 6) sets the width and height in inches.
    • fig is the overall container.
    • ax is the specific area where you draw your chart.

    Plotting the Matrix

    With an Axes object, you call:

    cax = ax.imshow(corr_matrix)
    

    This returns an image object (cax) that you can pass to the colorbar.


    Adding Axis Labels

    When using Axes, you don’t call plt.xticks() directly. Instead, you set ticks and labels on ax:

    ax.set_xticks(range(len(corr_matrix)))
    ax.set_xticklabels(corr_matrix.columns, rotation=45)
    ax.set_yticks(range(len(corr_matrix)))
    ax.set_yticklabels(corr_matrix.columns)
    

    info>Tip: Using rotation=45 again makes labels easier to read.


    Adding a Colorbar

    When you have a fig and ax, you must attach the colorbar to the figure:

    fig.colorbar(cax)
    

    Note: Passing cax ensures the color scale matches your heatmap.


    Showing the Plot

    Finally, display the complete figure:

    plt.show()
    

    Why This Approach Is Valuable

    • You get full control over size, layout, and styling.
    • Subplots make it easier to create more complex multi-panel layouts later.
    • Larger figure size improves clarity, especially when working with many variables.
  3. Challenge

    Step 3: Create a Heatmap with Seaborn

    Step 3: Build a Seaborn Heatmap

    Seaborn makes it easier to create polished, informative heatmaps with far less manual configuration than Matplotlib. In this step, you’ll transition from raw visualizations to professional-quality charts in just a few lines of code.

    You’ll learn how to:

    • Add annotations showing correlation values directly on the grid
    • Customize color palettes for better interpretation
    • Control cell shape, grid lines, and label styling
    • Finalize the heatmap with titles and colorbar labels

    This step will demonstrate why Seaborn is the go-to library for creating visually appealing heatmaps in Python.


    What You’ll Learn in This Step

    • Generate a Seaborn heatmap with numeric annotations
    • Apply custom color palettes and square cells
    • Add grid lines and adjust annotation font size
    • Rotate axis tick labels and add axis labels
    • Add titles and colorbar labels for a polished presentation

    Open the Notebook

    From the workspace panel, open the notebook file: 3-step-three.ipynb.

    info> Important: You must save your notebook (Ctrl/Cmd + S) before clicking Validate. Validation checks the most recent saved checkpoint.

    How to Complete Each Task
    • Find the matching code cell labeled Task 3.1, Task 3.2, etc.
    • Follow the # TODO comments and write your code step by step.
    • Run the cell using the Run button or by pressing Shift + Enter.
    • Save your progress using the Save icon or File > Save and Checkpoint.
    • All code and output will appear inline in the notebook.
    ### Creating a Seaborn Heatmap with Annotations

    Seaborn makes it much easier to create a polished heatmap compared to Matplotlib. Instead of manually setting colors and labels, Seaborn’s heatmap() function handles most of the work for you automatically.


    What sns.heatmap() Does

    • Visualizes a 2D matrix as a grid of colored cells.
    • Automatically labels rows and columns using DataFrame indexes and columns.
    • Can display the numeric values inside each cell using annot=True.

    Annotating Values

    Setting annot=True shows each correlation coefficient inside its cell. You can control the formatting with fmt:

    • fmt=".2f" rounds numbers to 2 decimal places.
    • Other formats (e.g., integers) are also supported.

    Example Usage
    sns.heatmap(
        matrix,
        annot=True,
        fmt=".2f"
    )
    

    This displays both the colors and the numeric correlation values.


    info> Tip: Even with annot=True, you should still include a colorbar to interpret the scale visually.


    Showing the Figure

    As with all Matplotlib and Seaborn plots, you need to call:

    plt.show()
    

    to render the heatmap.


    Note: The default color palette in Seaborn is "viridis", which provides a perceptually uniform scale.

    Applying a Custom Color Palette and Square Cells

    After creating your first annotated heatmap, you can improve its readability and visual appeal by customizing the colors and layout.


    Why Change the Colormap?

    The default colormap works, but it isn’t always the most intuitive. Using a diverging palette like "coolwarm" helps highlight positive vs. negative correlations more clearly.


    How to Apply a Custom Colormap

    Use the cmap argument in sns.heatmap() to change the color scheme.

    Example:

    cmap="coolwarm"
    

    Common Choices:

    • "coolwarm" – good for diverging values around 0
    • "viridis" – perceptually uniform, default in many tools
    • "magma" – darker background, high contrast

    Making the Cells Square

    By default, heatmap cells can look rectangular depending on your figure size. To make them perfectly square, set:

    square=True
    

    This helps align annotations neatly and improves symmetry.


    Combining Annotations, Colors, and Layout

    When you combine annotations, custom colors, and square cells, you get a much clearer and more professional result.

    Example usage (partial):

    sns.heatmap(
        data,
        annot=True,
        fmt=".2f",
        cmap="coolwarm",
        square=True
    )
    

    Note: If you want to explore other palettes, see the Seaborn documentation on color palettes. ### Adding Grid Lines and Adjusting Annotation Font Size

    Seaborn heatmaps can be refined to look cleaner and easier to read. Adding grid lines between cells and increasing annotation font size are simple ways to improve clarity and polish.


    Grid Lines Between Cells

    Use the linewidths and linecolor arguments to add visible dividers:

    • linewidths sets the thickness of the grid lines.
    • linecolor sets the color of the lines.

    This helps visually separate each cell, especially when using a strong color palette.


    Example
    sns.heatmap(
        matrix,
        annot=True,
        fmt=".2f",
        linewidths=1,
        linecolor="white"
    )
    

    Adjusting Annotation Font Size

    By default, annotations can appear small relative to the cell size. The annot_kws argument allows you to control their styling:

    • annot_kws={"size":10} increases the font size of the numbers inside the cells.

    Example
    sns.heatmap(
        matrix,
        annot=True,
        fmt=".2f",
        annot_kws={"size":10}
    )
    

    Tip: You can combine these options with cmap and square=True (from the previous tasks) to create a consistently styled heatmap. ### Rotating Axis Labels and Adding Titles

    Even with a styled heatmap, axis labels can overlap or look cluttered. Rotating and labeling your axes helps your visualization feel clear and professional.


    Rotating Tick Labels

    Matplotlib’s xticks() and yticks() let you rotate the text on each axis:

    • plt.xticks(rotation=45) rotates x-axis labels by 45 degrees.
    • plt.yticks(rotation=0) keeps y-axis labels horizontal.

    Rotating labels prevents them from overlapping and improves legibility.


    Example Usage
    plt.xticks(rotation=45)
    plt.yticks(rotation=0)
    

    Adding Axis Labels

    Use plt.xlabel() and plt.ylabel() to describe the meaning of each axis:

    plt.xlabel("Feature")
    plt.ylabel("Feature")
    

    Since a correlation matrix compares all features with each other, using the same label on both axes is common.


    info> Tip: You can call plt.xticks() and plt.xlabel() after sns.heatmap() to override any default settings.


    When to Rotate and Label

    Adding clear labels and adjusting rotation is an essential final step before sharing your chart:

    • Makes the visualization easier for other stakeholders to interpret.
    • Creates a polished look appropriate for presentations or reports. ### Final Touches: Titles and a Custom Colorbar Label

    Adding titles and labeling the colorbar are important finishing steps that make your heatmap presentation-ready. These adjustments improve clarity and help your audience understand exactly what they are looking at.


    Why Add a Title?

    A descriptive title provides context so viewers know what the chart shows without needing to read surrounding text.

    Example:

    plt.title("Correlation Heatmap of Marketing Metrics")
    

    This appears centered above the plot.


    Labeling the Colorbar

    The colorbar shows how colors map to numeric values. By default, it has no label—adding one clarifies what the colors represent.

    Use the cbar_kws parameter in sns.heatmap():

    cbar_kws={"label": "Correlation Coefficient"}
    

    This puts a label along the vertical color scale.


    Rotating and Labeling Axes

    Even with a title and colorbar, tick labels should be rotated and clear:

    • plt.xticks(rotation=45) tilts x-axis labels for readability.
    • plt.yticks(rotation=0) keeps y-axis labels horizontal.
    • plt.xlabel() and plt.ylabel() let you name each axis.

    Example:

    plt.xlabel("Feature X")
    plt.ylabel("Feature Y")
    

  4. Challenge

    Step 4: Customize Color Palettes

    Step 4: Refine Heatmap Styling and Color

    Once you’ve built a basic Seaborn heatmap, the next step is to fine-tune its appearance for clarity and impact. In this step, you’ll learn how to:

    • Choose color palettes that help viewers interpret values.
    • Label the colorbar to explain what the colors mean.
    • Normalize the color scale for comparability across charts.
    • Experiment with different palettes to improve perception.

    What You’ll Learn in This Step

    • Use diverging color palettes (like coolwarm) to highlight positive vs. negative correlations.
    • Label the colorbar so stakeholders understand the meaning of colors.
    • Control the color scaling with vmin and vmax for consistent interpretation.
    • Apply different palettes (e.g., magma, viridis) to see how color choices affect perception.

    Open the Notebook

    From the workspace panel, open the notebook file: 4-step-four.ipynb.

    info> Important: Save your notebook (Ctrl/Cmd + S) before clicking Validate. Validation checks the most recent saved checkpoint.

    How to Complete Each Task
    • Find the matching code cell labeled Task 4.1, Task 4.2, etc.
    • Follow the # TODO comments and write your code.
    • Run the cell using the Run button or by pressing Shift+Enter.
    • Save your work before validation.
    • All code and output will appear inline in the notebook.
    ### Applying a Diverging Color Palette

    A diverging color palette helps emphasize how correlations move from negative to positive values. This is especially useful when you care about both the strength and direction of relationships.


    What cmap Controls

    The cmap argument sets the color mapping for your heatmap:

    • It defines how numeric values (e.g., –1 to +1) translate into colors.
    • Diverging palettes have a neutral midpoint (usually 0) and two contrasting colors on either end.
    • This makes it easier to see which correlations are strongly positive vs. negative.

    Example Usage
    sns.heatmap(
        matrix,
        annot=True,
        fmt=".2f",
        cmap="coolwarm",
        square=True
    )
    

    In this example, cmap="coolwarm" uses blue for low values and red for high values.


    Common Diverging Color Palettes

    | Palette Name | Description | | ------------ | ---------------------------------------------- | | "coolwarm" | Blue to red gradient (popular for correlation) | | "RdBu" | Red–white–blue diverging | | "RdYlBu" | Red–yellow–blue | | "Spectral" | Multi-color diverging spectrum | | "PiYG" | Pink–yellow–green | | "BrBG" | Brown–blue-green |

    Tip: You can swap cmap to any of these names to experiment with different looks.


    Why This Matters

    • Diverging palettes show both direction and magnitude of correlation.
    • Neutral colors highlight values near zero.
    • Stakeholders can quickly see which relationships are strongly negative vs. positive.

    info> Tip: Always pick a palette with enough contrast so that weak and strong correlations are visually distinct. ### Labeling and Styling the Colorbar

    Colorbars help viewers interpret your heatmap’s color scale. Adding a label explains what the colors represent—making your visualization clearer and more professional.


    What cbar_kws Does

    The cbar_kws parameter lets you pass a dictionary of keyword arguments to customize the colorbar. This includes adding a label, changing orientation, scaling size, and adjusting spacing.


    Example Usage
    sns.heatmap(
        matrix,
        annot=True,
        fmt=".2f",
        cmap="coolwarm",
        square=True,
        cbar_kws={"label": "Correlation Coefficient"}
    )
    

    In this example:

    • "Correlation Coefficient" will appear as a label next to the colorbar.
    • The colorbar automatically scales to match your data range.

    info> Tip: Clear labels are essential if your audience isn’t familiar with the correlation scale.


    Common cbar_kws Options

    Here are a few other settings you can use to customize the colorbar:

    | Option | Description | Example | | ------------- | ---------------------------------- | ------------------------------ | | label | Text label beside the colorbar | "Correlation Coefficient" | | orientation | Direction of the colorbar | "vertical" or "horizontal" | | shrink | Scale factor for size | 0.8 | | pad | Space between heatmap and colorbar | 0.05 | | ticks | Custom tick positions | [-1, -0.5, 0, 0.5, 1] |


    Example combining options:

    sns.heatmap(
        matrix,
        annot=True,
        fmt=".2f",
        cmap="coolwarm",
        square=True,
        cbar_kws={
            "label": "Correlation",
            "orientation": "vertical",
            "shrink": 0.85,
            "pad": 0.04
        }
    )
    

    This approach makes your heatmap easier to interpret—and more polished for presentations or reports. ### Controlling Color Scaling with vmin and vmax

    By default, Seaborn scales colors based on the smallest and largest values in your matrix. This can make it harder to compare charts consistently—especially if your correlations are all positive or all negative.


    Why Set vmin and vmax?

    • vmin sets the minimum value in the colormap.
    • vmax sets the maximum value.
    • Fixing the range (e.g., –1 to +1) ensures that colors are comparable across different datasets or filtered subsets.

    Example Usage
    sns.heatmap(
        matrix,
        annot=True,
        fmt=".2f",
        cmap="coolwarm",
        vmin=-1,
        vmax=1,
        square=True
    )
    

    This makes the color scale consistent so:

    • +1 correlations are always darkest red.
    • –1 correlations are always darkest blue.

    info> Tip: When sharing charts, fixed scaling prevents confusion about what the colors represent. ### Experimenting with Alternate Color Palettes

    Choosing different colormaps can completely change how your data is perceived. Seaborn supports many palettes built into Matplotlib—some are better for accessibility or specific data ranges.


    Why Try Multiple Palettes?

    • Warm palettes (e.g., magma) can emphasize intensity.
    • Sequential palettes (e.g., viridis) are perceptually uniform and colorblind-friendly.
    • Diverging palettes (e.g., coolwarm) highlight both ends of a scale.

    Example Usage
    sns.heatmap(
        matrix,
        annot=True,
        fmt=".2f",
        cmap="magma",
        square=True
    )
    

    Common Palette Options

    Diverging Palettes

    • coolwarm
    • RdBu
    • BrBG

    Sequential Palettes

    • viridis
    • magma
    • plasma
    • cividis

    Perceptually Uniform

    • viridis (recommended)
    • cividis

  5. Challenge

    Step 5: Mask the Upper Triangle

    Step 5: Mask the Upper Triangle of the Heatmap

    When you create a correlation heatmap, the matrix is symmetric. Every value above the diagonal is a duplicate of one below it. Masking the upper triangle helps simplify the visualization by showing each correlation only once.

    In this step, you’ll build and apply a mask using NumPy to hide redundant values.


    What You’ll Learn in This Step

    • Create a mask with NumPy’s triu() function
    • Apply a mask to sns.heatmap() with the mask argument
    • Hide the diagonal to focus only on unique relationships

    Open the Notebook

    From the workspace panel, open the notebook file: 5-step-five.ipynb.

    info> Important: You must save your notebook (Ctrl/Cmd + S) before clicking Validate. Validation checks the most recent saved checkpoint.

    How to Complete Each Task
    • Find the matching code cell labeled Task 5.1, Task 5.2, etc.
    • Follow the # TODO comments and write your code step by step.
    • Run the cell using the Run button or by pressing Shift + Enter.
    • Save your progress using the Save icon or File > Save and Checkpoint.
    • All code and output will appear inline in the notebook.
    ### Using Masks to Hide Redundant Correlations

    Heatmaps often show symmetric correlation matrices, where each correlation appears twice (above and below the diagonal). For example:

    • Row “Ad Spend”, Column “CTR”
    • Row “CTR”, Column “Ad Spend”

    They are identical, making half the matrix redundant.

    Masking the upper triangle (or the diagonal too) makes your heatmap easier to read.


    What Is a Mask?

    A mask is a 2D Boolean array with the same shape as your matrix:

    • True: hide this cell
    • False: show this cell

    You create it with NumPy functions like np.triu().


    Why This Matters for Ad Spend and CTR

    Even when you mask the upper triangle, the Ad Spend vs CTR correlation remains visible in the lower triangle. This helps:

    • Focus attention on each unique relationship only once
    • Avoid duplicate labels and colors
    • Make your final visual cleaner for reports or presentations

    Example: Creating a Mask

    mask = np.triu(np.ones_like(corr_matrix, dtype=bool))
    
    • np.ones_like() creates a matrix of 1s matching the correlation matrix shape.
    • np.triu() converts the upper triangle to True.

    info> Tip: To hide the diagonal too, use:

    mask = np.triu(np.ones_like(corr_matrix, dtype=bool), k=0)
    

    Setting k=0 includes the diagonal in the mask.


    Note: Masking is purely visual—no correlations are removed from the data. ### Creating a Mask for the Upper Triangle

    Correlation matrices are symmetric, meaning the top-right and bottom-left halves are mirror images. To simplify the heatmap, you can create a mask to hide the upper triangle, reducing redundancy.


    Why Mask the Upper Triangle?

    • It makes the plot less crowded.
    • It highlights each unique correlation only once.
    • It’s a common best practice in publications.

    How to Create a Mask

    NumPy’s triu() function generates an array with 1s in the upper triangle and 0s elsewhere.

    • np.triu() returns the upper triangle (including the diagonal by default).
    • You can adjust which diagonal to include or exclude with the k parameter.

    Example: Creating a Basic Mask
    import numpy as np
    
    mask = np.triu(np.ones_like(corr_matrix, dtype=bool))
    
    • np.ones_like() creates an array of ones matching the shape of your correlation matrix.
    • dtype=bool ensures the mask is a boolean array.

    info> Tip: After creating your mask, you can print it to verify that the structure matches your expectations. ### Applying a Mask to Your Heatmap

    After creating the mask array, the next step is to apply it in sns.heatmap() so the upper triangle is hidden in the visualization.


    How Masking Works

    • The mask parameter takes a boolean array the same shape as your correlation matrix.
    • Any True values in the mask will hide that cell in the heatmap.
    • This lets you emphasize only the lower triangle of correlations.

    Example Usage
    sns.heatmap(
        matrix,
        mask=mask,
        annot=True,
        fmt=".2f",
        cmap="coolwarm",
        square=True
    )
    

    This code will only display the bottom triangle (including the diagonal).


    info> Tip: You can combine the mask with all prior styling options (color palette, annotations, grid lines) for a polished result.


    Note: If your mask is misaligned or the wrong shape, Seaborn will raise an error—make sure it exactly matches your matrix dimensions. ### Hiding the Diagonal for Maximum Clarity

    By default, np.triu() masks the upper triangle above the diagonal. If you want to hide the diagonal itself (so only unique correlations appear), you need to adjust the mask.


    How to Exclude the Diagonal

    • np.triu() accepts a k parameter that shifts which cells are masked:

      • k=0: Mask the diagonal and everything above.
      • k=1: Mask everything strictly above the diagonal (default behavior).
    • This makes your heatmap cleaner when the diagonal isn’t informative (it’s always 1.0).


    Example Usage
    mask = np.triu(np.ones_like(matrix, dtype=bool), k=0)
    

    This hides both the diagonal and upper triangle.


    info> Tip: When you hide the diagonal, your annotations will only show the unique correlation pairs below it.


    Note: This adjustment only affects the mask. All other styling (colormap, annotations) stays the same.

  6. Challenge

    Step 6: Final Presentation Polish

    Step 6: Final Presentation Polish

    Heatmaps are most effective when they’re both informative and visually clear. In this step, you’ll apply professional finishing touches to your Seaborn heatmap:

    • Create custom gradient palettes
    • Style text and annotations
    • Remove unnecessary chart borders
    • Add explanatory captions
    • Export a polished figure for reports

    These adjustments help you transition from a basic analytic visualization to a presentation-ready chart.


    What You’ll Learn in This Step

    • Build a custom diverging color palette with sns.diverging_palette()
    • Customize annotation fonts, colors, and titles for clarity
    • Remove spines and adjust whitespace to declutter the figure
    • Add captions and contextual notes with plt.figtext()
    • Save a high-resolution PNG suitable for sharing or publishing

    Open the Notebook

    From the workspace panel, open the notebook file: 6-step-six.ipynb.

    info> Important: Save your notebook (Ctrl/Cmd + S) before clicking Validate. Validation checks the most recent saved checkpoint.

    How to Complete Each Task
    • Locate the code cell labeled Task 6.1, Task 6.2, etc.
    • Follow the # TODO comments and write your code step by step.
    • Run each cell with the Run button or Shift + Enter.
    • Save your work before validation.
    • All output will appear inline in the notebook.
    ### Creating a Custom Gradient Palette

    Seaborn includes many built-in colormaps, but sometimes you need more control over the colors in your heatmap. The sns.diverging_palette() function lets you generate a gradient that highlights positive and negative values symmetrically around zero.


    Why Use a Diverging Palette?

    • Correlation matrices often have negative and positive values.
    • Diverging palettes make it obvious where the strongest positive and negative correlations are.
    • They can improve accessibility and interpretability.

    How sns.diverging_palette() Works

    This function returns a colormap object you can pass to cmap.

    Basic usage:

    cmap = sns.diverging_palette(
        h_neg, h_pos,
        as_cmap=True
    )
    

    Where:

    • h_neg: Hue for negative values (0–360 degrees).
    • h_pos: Hue for positive values.
    • as_cmap=True: Returns a continuous gradient.

    Example: Blue–Red Diverging Palette
    cmap = sns.diverging_palette(
        240,   # blue
        10,    # red
        as_cmap=True
    )
    

    Tip: Use sns.color_palette() instead for discrete color sets—diverging_palette() is better for gradients.


    ### Styling Annotations and Text Elements

    After you’ve built a clear heatmap, styling text and labels helps make it professional and readable.


    Customizing Annotation Text

    Use the annot_kws parameter to control how the annotation numbers look:

    • size: Controls font size.
    • color: Changes the text color.

    Example:

    annot_kws = {"size": 12, "color": "black"}
    

    Adding a Title

    Use plt.title() to add a clear heading:

    plt.title(
        "Correlation Heatmap of Marketing Variables",
        fontsize=16,
        fontweight="bold",
        color="darkblue"
    )
    

    Labeling the Axes

    Adding axis labels makes your chart self-explanatory:

    plt.xlabel("Metrics", fontsize=12)
    plt.ylabel("Metrics", fontsize=12)
    

    info> Tip: Consistent text styling helps stakeholders read your visualization more easily. ### Removing Spines and Adjusting Layout

    Removing chart borders and adjusting spacing helps create a minimalist, publication-ready look.


    Removing Spines

    Use sns.despine() to hide the top and right borders around your heatmap:

    sns.despine()
    

    This creates a cleaner visual by reducing unnecessary lines.


    Adjusting Layout

    If your labels or title overlap, you can adjust spacing with plt.tight_layout():

    plt.tight_layout()
    

    This automatically fits all elements neatly inside the figure.


    info> Tip: Combining sns.despine() and plt.tight_layout() ensures your chart looks polished without manual tweaking. ### Adding an Explanatory Caption Below the Chart

    Captions provide context about your visualization and help viewers interpret the meaning of the data.


    Why Use Captions?

    • They clarify thresholds or highlight important insights.
    • They make your visualization more self-explanatory when shared without additional commentary.

    Adding a Caption with plt.figtext()

    Use plt.figtext() to place a text box anywhere in the figure. The most common placement is below the chart:

    plt.figtext(
        x=0.5,               # Centered horizontally
        y=-0.05,             # Below the chart
        s="Correlation >0.7 indicates strong association",
        ha="center",         # Center-aligned text
        fontsize=10
    )
    

    info> Tip: Adjust y to move the text closer or further from the figure. Negative values place it below the bottom edge.

What's a lab?

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.