ANALYSIS TOOL BOX

Visualizations

PlotBarChart

Generate a formatted horizontal bar chart for categorical data comparison.

bar-chartvisualizationcategoricalcomparison

Introduction

When the question is "which categories stand out, and by how much?" — which departments drive admissions, which strains dominate an outbreak, which features carry a model — a horizontal bar chart answers it faster than a table of numbers. PlotBarChart produces a clean, presentation-ready comparison from a single DataFrame: it sorts categories by value, wraps long labels, and can highlight the top N so the signal you care about reads at a glance. Reach for it to turn categorical counts or scores into a figure a stakeholder can act on, with titles, captions, and a data-source annotation for defensible reporting.

Teaching Note

Horizontal bar charts are essential for:

  • Epidemiology: Comparing disease incidence rates across different age groups or districts.
  • Healthcare: Comparing the number of patient admissions across different medical departments.
  • Data Science: Comparing feature importance scores or model performance metrics.
  • Public Health: Visualizing the distribution of health resource allocations.
  • Marketing: Comparing customer preference scores for different product categories.
  • Finance: Visualizing budget allocations across various operational departments.
  • Social Science: Presenting survey response counts for multiple-choice questions.

Parameters

ParameterTypeDefaultDescription
dataframerequiredpd.DataFrameThe pandas DataFrame containing the categorical and numeric data to plot.
categorical_column_namerequiredstrThe name of the column representing the categories (y-axis).
value_column_namerequiredstrThe name of the column representing the numeric values (x-axis).
fill_colorstr'#8eb3de'A hex color code used for all bars. Applied when color_palette and top_n_to_highlight are not set.
color_palettestr | listNoneA seaborn palette name (e.g., 'Set2') or a list of colors to use for the bars.
top_n_to_highlightintNoneNumber of highest-value categories to highlight; other bars render in neutral gray.
highlight_colorstr'#b0170c'The hex color code used for highlighted bars.
fill_transparencyfloat0.8The alpha (transparency) of the bar fill, from 0 to 1.
display_order_listlistNoneExplicit category order (top to bottom). If None, bars sort by value descending.
figure_sizetuple(8, 6)The (width, height) of the figure in inches.
title_for_plotstrNoneThe primary title text displayed at the top of the chart.
subtitle_for_plotstrNoneDescriptive subtitle text displayed below the title.
caption_for_plotstrNoneDescriptive text or notes displayed at the bottom of the plot.
data_source_for_plotstrNoneText identifying the source of the data, appended to the caption.
title_y_indentfloat1.15Vertical offset for the title position relative to the axes.
subtitle_y_indentfloat1.1Vertical offset for the subtitle position relative to the axes.
caption_y_indentfloat-0.15Vertical offset for the caption position relative to the axes.
decimal_places_for_data_labelint1Number of decimal places in the value label at the end of each bar.
data_label_fontsizeint11Font size for the numeric labels next to the bars.
data_label_paddingfloatNoneHorizontal gap between the bar end and its label. If None, uses 10% of the max value.
filepath_to_save_plotstrNoneLocal path ending in .png or .jpg to save the plot. If None, the figure is not saved.

Example

python
from analysistoolbox.visualizations import PlotBarChart
import pandas as pd

# Epidemiology: incidence of influenza strains
flu_data = pd.DataFrame({
    'Virus Strain': ['A(H1N1)', 'A(H3N2)', 'B/Victoria', 'B/Yamagata'],
    'Confirmed Cases': [1250, 840, 310, 120],
})

PlotBarChart(
    flu_data, 'Virus Strain', 'Confirmed Cases',
    top_n_to_highlight=1,
    title_for_plot="Influenza Strain Distribution",
    subtitle_for_plot="Total confirmed cases for the current flu season",
)