ANALYSIS TOOL BOX

Visualizations

PlotSingleVariableHistogram

Generate a formatted histogram to visualize the distribution of a single numeric variable.

visualizationchartsplottinggraphs

Introduction

Is this variable roughly normal, skewed, bimodal, or full of outliers — and where do the mean and median sit relative to each other? Those shape questions determine which statistical tests and transformations are appropriate downstream, and a histogram answers them faster than any summary statistic. PlotSingleVariableHistogram renders the frequency distribution of a numeric column with optional mean and median reference lines, giving an immediate read on central tendency and skew.

Teaching Note

Single-variable histograms are essential for:

  • Epidemiology: Visualizing the distribution of patient ages in a disease outbreak.
  • Healthcare: Analyzing the distribution of systolic blood pressure readings across a population.
  • Intelligence Analysis: Monitoring the distribution of travel distances for suspected illicit transport.
  • Data Science: Inspecting numeric features for skewness, outliers, and normalization requirements.
  • Public Health: tracking the distribution of daily water consumption per capita.
  • Finance: Examining the distribution of trade transaction sizes in a specific portfolio.
  • Project Management: Analyzing the distribution of task completion times across a project lifecycle.
  • Social Science: Visualizing the distribution of household income levels in a surveyed region.

Parameters

ParameterTypeDefaultDescription
dataframerequiredpd.DataFrameThe pandas DataFrame containing the numeric variable to analyze.
value_column_namerequiredstrThe name of the continuous numeric column to be plotted on the x-axis.
fill_colorstr'#999999'The hex color code for the histogram bars. Defaults to '#999999'.
fill_transparencyfloat0.6The transparency level (alpha) of the histogram bars (0 to 1). Defaults to 0.6.
show_meanboolTrueWhether to overlay a dashed vertical line and text label indicating the arithmetic mean of the data. Defaults to True.
show_medianboolTrueWhether to overlay a dotted vertical line and text label indicating the median of the data. Defaults to True.
title_for_plotstrNoneThe primary title text displayed at the top of the chart. Defaults to None.
subtitle_for_plotstrNoneDescriptive subtitle text displayed below the main title. Defaults to None.
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.
show_y_axisboolFalseWhether to display the vertical frequency (count) axis. Defaults to False.
title_y_indentfloat1.1Vertical offset for the title position relative to the axes. Defaults to 1.1.
subtitle_y_indentfloat1.05Vertical offset for the subtitle position relative to the axes. Defaults to 1.05.
caption_y_indentfloat-0.15Vertical offset for the caption position relative to the axes. Defaults to -0.15.
figure_sizetuple(8, 6)Dimensions of the output figure as a (width, height) tuple in inches. Defaults to (8, 6).
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 histogram using matplotlib and optionally saves it to disk.

Example

python
from analysistoolbox.visualizations import PlotSingleVariableHistogram
import pandas as pd
import numpy as np

# Epidemiology: age distribution of study participants
epi_df = pd.DataFrame({'Age': np.random.normal(45, 12, 500)})
PlotSingleVariableHistogram(
    epi_df, 'Age',
    fill_color="#27ae60",
    title_for_plot="Participant Age Distribution",
    subtitle_for_plot="Samples collected from regional demographic survey (N=500)"
)