ANALYSIS TOOL BOX

Visualizations

PlotCorrelationMatrix

Generate a correlation matrix or multi-variable pairplot.

correlationpairplotedavisualizationrelationship

Introduction

A correlation coefficient tells you the strength of a linear relationship, but it silently misses U-shapes, thresholds, and other nonlinear patterns that a scatter plot would reveal instantly. PlotCorrelationMatrix gives you both views: a printed numeric correlation matrix for a quick scan, or a full pairplot with scatter plots and optional LOWESS trend lines for every variable pair, optionally narrowed to just the relationships with a specific outcome column so a large predictor set stays readable.

Teaching Note

Correlation matrices and pairplots are essential for:

  • Epidemiology: Examining the relationships between multiple environmental factors and disease incidence.
  • Healthcare: Analyzing correlations between different patient vital signs and recovery outcomes.
  • Data Science: Identifying multi-collinearity during feature selection and exploratory data analysis.
  • Public Health: Correlating various socioeconomic indicators with regional life expectancy.
  • Finance: Analyzing the price movements of multiple assets to assess portfolio diversification.
  • Quality Control: Examining the relationship between different manufacturing sensor readings and defect counts.
  • Social Science: Exploring the connections between multiple demographic variables and survey scores.

Parameters

ParameterTypeDefaultDescription
dataframerequiredpd.DataFrameThe pandas DataFrame containing the numeric data to analyze.
list_of_value_column_namesrequiredlist of strThe names of the columns to be used on the x-axis of the pairplot (or the entire variable set if list_of_outcome_column_names is None).
list_of_outcome_column_nameslist of strNoneThe names of the outcome columns to be used on the y-axis of the pairplot. If None, a symmetric grid is created using all value columns. Defaults to None.
show_as_pairplotboolTrueWhether to generate the visual pairplot. If False, the function prints the numeric correlation matrix to the console instead. Defaults to True.
pairplot_sizetuple(20, 20)The dimensions of the entire figure grid in inches. Defaults to (20, 20).
scatter_fill_colorstr'#3269a8'The hex color code for the scatter plot points and diagonal distributions. Defaults to '#3269a8'.
fit_lowess_lineboolTrueWhether to fit a Locally Weighted Scatterplot Smoothing (LOWESS) line to the scatter plots to show non-linear trends. Defaults to True.
line_colorstr'#cc4b5a'The hex color code for the regression or LOWESS line. Defaults to '#cc4b5a'.
title_for_plotstr'Pairplot of Numeric Variables'The primary title text displayed at the top of the figure. Defaults to 'Pairplot of Numeric Variables'.
subtitle_for_plotstr'Shows the linear relationship between numeric variables'Descriptive subtitle text displayed below the title.
caption_for_plotstrNoneExplanatory text or notes displayed at the bottom of the plot. Defaults to None.
data_source_for_plotstrNoneText identifying the source of the data, appended to the caption. Defaults to None.
x_indentfloat-0.7Horizontal offset for titles and captions relative to the axes. Defaults to -0.7.
title_y_indentfloat1.12Vertical offset for the title position relative to the grid. Defaults to 1.12.
title_font_sizeint14Font size for the primary title. Defaults to 14.
subtitle_y_indentfloat1.03Vertical offset for the subtitle position relative to the grid. Defaults to 1.03.
subtitle_font_sizeint11Font size for the subtitle. Defaults to 11.
caption_y_indentfloat-0.35Vertical offset for the caption position relative to the grid. Defaults to -0.35.
caption_font_sizeint8Font size for the caption text. Defaults to 8.
filepath_to_save_plotstrNoneThe local path (ending in .png or .jpg) where the plot should be exported. If None, the file is not saved. Defaults to None.

Returns

None — the function displays the plot using matplotlib (or prints a correlation matrix) and optionally saves the visual output to disk.

Example

python
from analysistoolbox.visualizations import PlotCorrelationMatrix

PlotCorrelationMatrix(
    dataframe=df,
    list_of_value_column_names=['age', 'income', 'credit_score', 'loan_amount'],
    list_of_outcome_column_names=['default_risk'],
    fit_lowess_line=True,
    title_for_plot='Credit Risk Feature Relationships',
)