Programming for Analytics

Chapter 9
Exploratory Analysis and Data Visualization

Post Graduate Program in Business Analytics
Programming for Analytics   •   Post Graduate

Exploratory Analysis and Data Visualization

Introduction

Exploratory data analysis (EDA) is the disciplined investigation of data before formal modelling or decision-making. It combines statistical summaries, visual reasoning, domain knowledge, and computational methods to discover structure, detect data-quality problems, evaluate assumptions, and generate useful questions. Data visualization converts analytical results into perceptible patterns so that analysts, managers, and other stakeholders can interpret evidence and act on it.

EDA is not a preliminary activity that can be skipped once a predictive model is available. A model trained on duplicated records, incorrectly coded categories, leakage, or a biased sample may produce highly accurate-looking but invalid results. Similarly, a dashboard can communicate technically correct values while encouraging incorrect conclusions through inappropriate scales, selective filtering, or visual clutter. Professional analytics therefore requires both statistical integrity and communication design.

This chapter presents a systematic workflow for EDA, principles for effective statistical graphics and dashboards, interaction and accessibility considerations, and methods for avoiding misleading visual narratives.

Learning objectives

By the end of this chapter, a learner should be able to:

  1. Design a reproducible EDA workflow for structured business data.
  2. Select appropriate summaries and visual encodings for variable types and analytical questions.
  3. Diagnose missingness, outliers, skewness, dependence, and potential data leakage.
  4. Interpret relationships without confusing association with causation.
  5. Design dashboards that support decisions rather than merely display metrics.
  6. Apply interaction, accessibility, annotation, and uncertainty principles.
  7. Identify misleading or ethically problematic visual communication.
  8. Translate exploratory findings into defensible analytical recommendations.

1. The logic of exploratory data analysis

EDA is an iterative process rather than a fixed sequence of charts. A useful cycle is:

Question → inspect → clean and document → summarize → visualize → investigate anomalies → revise question → communicate.

The analyst should begin with the decision context. “What happened to revenue?” is less useful than “Which customer segments and regions explain the quarter-on-quarter change in gross margin?” The second question determines the required measures, comparison groups, time period, and level of aggregation.

1.1 Data structure and analytical grain

Before calculating a statistic, identify the unit represented by each row. A table may contain one row per transaction, customer, invoice, store-day, or monthly region. This is the analytical grain. Joining tables at incompatible grains can multiply records and inflate totals. For example, joining one customer row to many transaction rows and then summing a customer-level annual target may count the target repeatedly.

Important structural questions include:

  • What does one observation represent?
  • Which columns are identifiers, measures, dimensions, dates, or derived fields?
  • Are identifiers unique where they should be?
  • Which variables are categorical, ordinal, discrete numerical, continuous numerical, or temporal?
  • Are records independent, repeated measurements, or nested within groups?
  • What population and time period do the data represent?

A data dictionary should record definitions, units, permissible values, source systems, transformations, and ownership. The distinction between a missing value, a genuine zero, and “not applicable” is especially important.

1.2 A reproducible EDA workflow

A professional workflow normally includes:

  1. Frame the decision. Define the audience, outcome, time horizon, and action being supported.
  2. Profile the data. Inspect dimensions, types, ranges, duplicate keys, missingness, and category frequencies.
  3. Validate quality. Check constraints such as non-negative quantity, valid dates, unique order identifiers, and consistent currencies.
  4. Understand distributions. Examine location, spread, skewness, tails, and unusual concentrations.
  5. Compare groups. Segment by relevant dimensions while preserving sample sizes and denominators.
  6. Investigate relationships. Use plots and measures appropriate to variable types and design.
  7. Test robustness. Compare results under defensible cleaning rules, alternative aggregations, and relevant subgroups.
  8. Record decisions. Preserve code, assumptions, exclusions, chart specifications, and interpretations.

EDA should be performed on a development copy while retaining an immutable raw dataset. Every deletion, imputation, recoding, and aggregation should be traceable.

2. Descriptive statistics for exploration

Descriptive statistics summarize observed data; they do not automatically generalize to a wider population. Let numerical observations be x1, x2, ..., xn, where n is the number of non-missing observations.

The arithmetic mean is:

x̄ = (1/n) ∑ᵢ₌₁ⁿ xᵢ

The mean is sensitive to extreme observations and is appropriate when values are reasonably symmetric or when the total is substantively meaningful. The median is the middle ordered observation, or the average of the two middle observations when n is even. It is more resistant to extreme values and is often preferable for income, transaction value, and response-time data.

The range is max(xᵢ) − min(xᵢ). The sample variance and sample standard deviation are:

s² = [∑ᵢ₌₁ⁿ (xᵢ − x̄)²] / (n − 1)

s = √s²

Here, s² is the sample variance and s is the sample standard deviation, in squared units and original units respectively. The denominator n − 1 is used when estimating population variance from a sample under the usual independent-sampling framework. For a complete finite population, a population variance convention using denominator n may be appropriate.

The interquartile range is:

IQR = Q₃ − Q₁

where Q₁ and Q₃ are the first and third quartiles. A common exploratory rule flags values below Q₁ − 1.5IQR or above Q₃ + 1.5IQR as potential outliers. This is a screening convention, not proof that an observation is erroneous. Outliers may be genuine high-value customers, fraud, system errors, or observations from a different process.

For a category with count fj out of n valid records, its relative frequency is:

pj = fj / n

Percentages must state their denominator. “Conversion increased to 8%” is incomplete unless the population, time period, and definition of conversion are clear.

2.1 Distributional shape

Histograms, empirical cumulative distribution plots, box plots, and density plots reveal concentration and tails. A histogram depends on bin width; different widths can suggest different stories. Density plots can obscure sample size and may be inappropriate for small or highly discrete datasets. Box plots efficiently compare groups but hide multimodality and sample size. Good practice is to combine a compact summary with a plot and, where relevant, show individual observations.

Skewness matters because mean-based comparisons can be dominated by a long tail. For right-skewed monetary values, analysts may inspect the median, percentiles, and a logarithmic display. A transformed display must be labelled clearly and should not conceal the original units.

3. Exploring relationships

The choice of graphic should follow the types of variables and the question.

Analytical taskSuitable first choicesImportant cautions
Compare category totalsOrdered bar chart, dot plotShow denominator and avoid excessive categories
Compare distributionsBox plot, violin plot, histogram, jittered pointsDisplay sample size and avoid overinterpreting smooth density
Examine two numerical variablesScatter plot, hexbin plotCheck overplotting, nonlinearity, and influential points
Examine timeLine chart, small multiplesPreserve time order and distinguish stock from flow
Examine composition100% stacked bar, slope chartComposition can hide changes in total size
Examine geographic variationSymbol or choropleth mapAvoid area-based distortion and unstable small denominators

3.1 Association and correlation

Covariance measures joint deviation from means. Sample correlation is:

r = [∑ᵢ₌₁ⁿ (xᵢ − x̄)(yᵢ − ȳ)] / √[∑ᵢ₌₁ⁿ (xᵢ − x̄)² × ∑ᵢ₌₁ⁿ (yᵢ − ȳ)²]

Here, xᵢ and yᵢ are paired observations, and x̄ and ȳ are their sample means. The statistic is dimensionless and lies between −1 and 1 when both variables have non-zero variance. It measures linear association, not causation. It can be distorted by outliers, restricted ranges, nonlinear relationships, and clustered observations.

A scatter plot should precede interpretation of correlation. A near-zero correlation can coexist with a strong U-shaped relationship. Conversely, a high correlation may be caused by a shared time trend or a third variable. Segmenting a plot can reveal Simpson’s paradox, in which an aggregated relationship reverses or disappears within groups.

3.2 Time-series exploration

Time data require attention to ordering, seasonality, missing periods, changing definitions, and revisions. A line chart is generally suitable for a continuous measure over ordered time, but multiple series can become unreadable. Small multiples often provide clearer comparisons.

Distinguish a flow, such as monthly sales, from a stock, such as customers at month-end. Summing a stock across months may be meaningless, while averaging or taking an end-of-period value may be appropriate. Comparisons should use consistent periods and, where necessary, normalize for business days, population, exposure, or store count.

4. Data quality, missingness, and outliers

Missingness can arise from nonresponse, system failure, delayed integration, structural non-applicability, or deliberate suppression. Three conceptual mechanisms are commonly discussed:

  • MCAR: missingness is unrelated to observed and unobserved values.
  • MAR: missingness depends on observed information, conditional on which analysis may adjust.
  • MNAR: missingness depends on unobserved values even after considering observed information.

These are assumptions about a data-generating process, not labels that can always be proven from the dataset. Compare missingness rates across time, products, regions, and outcome groups. A missingness indicator can itself be informative; for example, a delayed approval may predict later cancellation.

Imputation should not be treated as a universal repair. Replacing missing income with the mean reduces apparent variation and can bias relationships. More principled methods may include group-specific, multiple, or model-based imputation, but their assumptions must be documented. Sometimes the correct action is to retain a missing category, exclude a variable, or obtain better source data.

Outlier handling should follow a decision rule. First verify whether the value is a data-entry or measurement error. If valid, consider robust summaries, transformation, winsorization, segmentation, or a model suited to heavy tails. Silent deletion is unacceptable because it can alter conclusions and introduce bias.

5. Visual encoding and graphical integrity

A visualization maps data values to visual attributes. Position along a common scale is generally more accurately judged than angle, area, volume, colour intensity, or shape. This is why aligned dot plots and bar charts are often preferable to three-dimensional effects and decorative pie charts.

5.1 Scales, axes, and baselines

Bar charts encode magnitude through length from a baseline, so a zero baseline is normally required. Truncating a bar axis can exaggerate differences. A line chart may use a non-zero vertical range when the purpose is to inspect small changes, but the axis break or range must be explicit and the design must not imply a larger absolute difference than exists.

Dual axes can create false visual alignment between unrelated measures. If two scales are essential, explain them explicitly and consider separate aligned panels instead. Logarithmic scales are valid for multiplicative relationships and wide ranges, but every axis must clearly indicate the transformation. A log scale cannot represent zero or negative values without a special treatment.

5.2 Colour, annotation, and uncertainty

Use colour to encode a meaningful variable or to direct attention to a decision-relevant exception. Sequential palettes suit ordered magnitudes; diverging palettes suit values around a meaningful midpoint; qualitative palettes suit unordered categories. Do not rely on red-versus-green alone because colour-vision deficiencies are common. Use labels, symbols, line styles, or patterns as reinforcement.

Annotations should explain an important event, threshold, or comparison rather than repeat every label. Uncertainty may be represented by confidence intervals, prediction intervals, quantile bands, error bars, or explicit ranges. These are not interchangeable. A confidence interval concerns uncertainty about a parameter under a specified inferential procedure; a prediction interval concerns the distribution of a future observation and is usually wider.

6. Dashboard and visual narrative design

A dashboard is an interface for monitoring, diagnosis, or exploration. It is not simply a collection of charts. A useful dashboard has a defined audience, decision frequency, data refresh policy, metric definitions, and action pathway.

A practical hierarchy is:

  1. Orientation: title, time period, scope, and key definitions.
  2. Status: a small number of carefully defined headline indicators.
  3. Diagnosis: trends, segment comparisons, drivers, and exceptions.
  4. Action: alerts, responsible owners, drill-through details, or recommended next steps.

Key performance indicators should include a numerator, denominator, unit, time window, comparison baseline, and target definition. For example, “retention” could mean customers active after 30 days, revenue retained, or accounts not cancelled. A number without a definition is not a reliable metric.

6.1 Interaction design

Filters should have sensible defaults, show their current state, and avoid silently changing denominators. Cross-filtering must be predictable. Drill-down should preserve context and provide a route back to the overview. Tooltips can provide exact values and definitions, but essential information should not be hidden exclusively in hover interactions because hover is unavailable or difficult on many mobile and assistive technologies.

Avoid excessive interactivity. Every control should answer a recognizable analytical question. A dashboard with dozens of filters can shift cognitive burden to the user and encourage arbitrary storytelling. Provide reset controls, clear “no data” states, and warnings when a selected subgroup is too small for a stable interpretation.

6.2 Accessibility

Accessible visualization is both an ethical requirement and a quality practice. Use sufficient contrast, readable text, keyboard-operable controls, descriptive titles, logical focus order, and alternative text or a textual summary for important graphics. Do not encode meaning through colour alone. Charts should remain interpretable when printed in grayscale where practical. Screen-reader users need data tables, summaries, or structured descriptions for complex graphics.

Accessibility also includes cognitive accessibility: consistent layouts, plain metric definitions, limited simultaneous visual elements, and clear error messages. Compliance should be checked against applicable organizational policy and recognized standards such as WCAG, while also conducting user testing with people who use assistive technologies.

7. Avoiding misleading communication

Misleading communication can result from inaccurate data, inappropriate analysis, or technically true but manipulative presentation. Common failures include:

  • omitting the denominator or base population;
  • comparing percentages with different sample sizes;
  • presenting cumulative totals as periodic performance;
  • using a truncated bar axis to exaggerate change;
  • selecting only a favourable time window;
  • hiding missing categories or exclusions;
  • treating correlation as a causal effect;
  • using a map when rates are unstable or areas differ greatly in size;
  • smoothing a volatile series without showing the raw observations;
  • reporting a relative increase without the absolute baseline;
  • implying precision greater than the measurement supports.

A robust visual narrative states the scope, source, definitions, transformations, limitations, and relevant uncertainty. Analysts should show counterevidence when it materially changes interpretation. Ethical visualization does not require displaying every observation in every executive view, but it does require that omissions do not create a false impression.

8. Implementation pattern in Python

A reproducible implementation typically separates data validation, transformation, analysis, and presentation. A conceptual pattern is:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

raw = pd.read_csv("orders.csv")
df = raw.copy()

# Validate before aggregation
assert df["order_id"].notna().all()
assert (df["quantity"] >= 0).all()
df["order_date"] = pd.to_datetime(df["order_date"], errors="coerce")

profile = df.isna().mean().sort_values(ascending=False)
monthly = (df.dropna(subset=["order_date"])
             .assign(month=lambda x: x["order_date"].dt.to_period("M").dt.to_timestamp())
             .groupby("month", as_index=False)
             .agg(revenue=("revenue", "sum"), orders=("order_id", "nunique")))

sns.lineplot(data=monthly, x="month", y="revenue", marker="o")
plt.xlabel("Month")
plt.ylabel("Revenue in reporting currency")
plt.title("Monthly revenue")
plt.tight_layout()
plt.show()

The code is only an implementation pattern. Production work should add schema validation, explicit currency and timezone handling, tests for duplicate keys, versioned dependencies, and controlled publication. A chart should never be considered validated merely because the code executes without error.

9. Applied case: an illustrative retail operations dashboard

Background

A fictional omnichannel retailer reports declining quarterly profit despite stable sales. Executives request a dashboard for regional managers.

Problem

An initial dashboard shows total revenue, average order value, and a red downward arrow for profit. Managers argue that the West region is underperforming, but the dashboard does not show returns, fulfilment cost, order volume, or denominators.

Analysis

The analytics team first establishes the grain: one row per order line. They identify duplicated order identifiers caused by a many-to-many promotion join and correct the aggregation. They then define gross margin as revenue minus product cost and fulfilment cost, divided by revenue, with returns deducted from revenue. They examine distributions and discover that a small number of bulk business orders raise the mean order value.

A segment analysis shows that the West region has higher revenue growth than other regions but also a rapidly increasing return rate. The return rate is calculated as returned orders divided by shipped orders, with the denominator shown for each region. A time-series view reveals that the change begins after a packaging supplier switch. However, the association is not treated as proof of causation; operational records and a controlled supplier review are required.

Approach and solution

The revised dashboard contains:

  • headline cards for revenue, gross margin percentage, orders, and return rate;
  • a trend chart with the selected period and prior-period comparison;
  • a region-by-channel matrix showing numerator, denominator, and rate;
  • a distribution view of order value using median and percentiles;
  • an annotated timeline for the packaging change;
  • filters for period, region, channel, and product category;
  • definitions, refresh timestamp, data-quality warnings, and an accessible table view.

Small regions are flagged when their return-rate estimates are unstable. The dashboard distinguishes absolute profit change from percentage change and allows managers to inspect order-level evidence without exposing unnecessary personal data.

Outcome and learning points

The dashboard redirects attention from a misleading regional revenue comparison to a fulfilment and returns problem. Operations initiates packaging tests and monitors return rates, margin, and customer complaints. The principal lesson is that dashboard value comes from valid metric definitions, correct grain, contextual comparisons, and a clear decision pathway—not from the number of charts.

10. Advanced and contemporary considerations

Modern EDA increasingly involves high-volume event data, embedded analytics, geospatial data, and automated insight generation. Machine-learning systems can identify anomalies, clusters, or unusual changes, but automated findings remain hypotheses requiring domain validation. An anomaly detector may flag a legitimate promotion, a data pipeline failure, or a rare but important event.

Analysts should distinguish exploratory findings from confirmatory claims. Repeatedly inspecting many segments increases the likelihood of finding an apparently unusual pattern by chance. Promising findings should be tested on held-out periods, new samples, or preregistered confirmatory analyses where feasible. Data and model lineage, access controls, privacy protection, and audit logs become especially important when dashboards influence credit, employment, healthcare, pricing, or public services.

Privacy-preserving aggregation may be necessary when small cells could reveal individuals. Suppression thresholds, differential privacy, de-identification, and role-based access should be selected with security and statistical utility in mind. Removing direct identifiers does not guarantee anonymity when multiple datasets can be linked.

Best-practice checklist

Before publishing an exploratory analysis or dashboard, ask:

  • Is the analytical grain explicit and is the source data validated?
  • Are measures, denominators, units, and time windows defined?
  • Are missing values, exclusions, and outliers documented?
  • Does each visual match the variable types and analytical question?
  • Are axes, transformations, baselines, and uncertainty clear?
  • Can users identify the current filters and return to a default view?
  • Is the presentation accessible without relying on colour or hover?
  • Does the narrative distinguish association from causation?
  • Have subgroup sizes, alternative explanations, and counterevidence been considered?
  • Are privacy, governance, refresh, ownership, and audit requirements addressed?

Chapter Summary

Exploratory data analysis is an iterative, question-driven process that links data validation, descriptive statistics, visualization, and domain reasoning. Reliable EDA begins with the analytical grain and data dictionary, then examines distributions, missingness, outliers, time structure, and relationships. Mean, median, variance, standard deviation, interquartile range, frequency, and correlation are useful summaries only when their assumptions and limitations are understood.

Effective visualization uses encodings that support accurate comparison, appropriate scales, meaningful colour, explicit denominators, and visible uncertainty. Dashboards require metric governance, interaction discipline, accessibility, and an action pathway. Analysts must actively prevent misleading communication by documenting scope, transformations, exclusions, and uncertainty, and by avoiding causal claims from observational association. Contemporary tools, including AI-assisted anomaly detection, can accelerate exploration but cannot replace validation, domain expertise, ethical judgment, or reproducible evidence.

Key Terms

Analytical grain: The unit represented by one row or observation in a dataset.

Exploratory data analysis: Systematic investigation of data to discover structure, quality issues, patterns, and questions.

Data dictionary: Documentation of fields, definitions, units, types, sources, and permissible values.

Mean: Arithmetic average of numerical observations.

Median: Middle value of ordered observations.

Variance: Average squared deviation used to quantify dispersion; sample variance commonly uses denominator n − 1.

Standard deviation: Square root of variance, expressed in the original measurement units.

Interquartile range: Difference between the third and first quartiles.

Outlier: An observation unusually distant from the rest or inconsistent with an expected process.

Missingness: Absence of an observed value, whether caused by non-applicability, failure, or another process.

Correlation: Standardized measure of linear association between paired numerical variables.

Simpson’s paradox: Reversal or alteration of an aggregated relationship after data are separated into groups.

Small multiples: Repeated comparable charts using separate panels for categories or segments.

Dashboard: Interactive visual interface designed to support monitoring, diagnosis, or decisions.

KPI: Key performance indicator defined with a measure, denominator, scope, and comparison or target.

Visual encoding: Mapping of data values to position, length, colour, shape, or other graphical attributes.

Confidence interval: An interval produced by an inferential procedure to express uncertainty about a population parameter.

Prediction interval: An interval intended to describe uncertainty for a future individual observation.

Data leakage: Use of information in analysis or modelling that would not be available at the decision time.

Visual narrative: Structured communication that connects evidence, context, interpretation, and action.

Review Questions

Short Answer Questions

  1. Why is analytical grain important when joining and aggregating data?
  2. Distinguish a median from a mean in the presence of extreme values.
  3. Why does correlation not establish causation?
  4. State two accessibility requirements for dashboards.
  5. Why should a dashboard metric include its denominator and time window?

Long Answer Questions

  1. Explain a complete reproducible EDA workflow, including validation, documentation, visualization, and robustness checks.
  2. Compare histograms, box plots, scatter plots, line charts, and choropleth maps, describing when each is appropriate and what can mislead interpretation.
  3. Discuss missing-data mechanisms and evaluate the risks of mean imputation, deletion, and model-based imputation.
  4. Explain how scale, colour, annotation, and uncertainty affect the integrity of visual communication.
  5. Design an executive dashboard for a declining-profit business. Specify its audience, metrics, visual components, interactions, and governance controls.

Analytical / Application Questions

  1. A region reports a 20% increase in sales but a 2 percentage-point fall in conversion. Explain what additional denominators, segmentations, and time comparisons are required before recommending action.
  2. A scatter plot shows a strong positive relationship between advertising expenditure and sales across twelve months. Develop an EDA plan to assess whether seasonality, trend, reverse causality, or a third variable could explain the relationship.
  3. A dashboard shows a dramatic increase in average order value after a promotion. Propose checks for duplicated records, changed product mix, bulk orders, denominator effects, and distributional changes before concluding that customer value improved.

Multiple Choice Questions with Answers

Question 1

Which measure is generally most resistant to extreme observations?

A. Arithmetic mean

B. Median

C. Variance

D. Range

Correct Answer: B. Median

Question 2

What is the main purpose of identifying analytical grain?

A. To choose a colour palette

B. To determine the unit represented by each observation

C. To maximize the number of dashboard filters

D. To replace missing values

Correct Answer: B. To determine the unit represented by each observation

Question 3

A correlation coefficient primarily measures which relationship?

A. Causal impact

B. Linear association

C. Difference in group medians

D. Forecast accuracy

Correct Answer: B. Linear association

Question 4

Which chart is usually most appropriate for showing a continuous measure over ordered time?

A. Line chart

B. Pie chart

C. Unordered categorical bar chart

D. Three-dimensional surface chart

Correct Answer: A. Line chart

Question 5

For a standard bar chart encoding magnitude by length, which baseline is normally appropriate?

A. The maximum observed value

B. Zero

C. The median only

D. An arbitrary positive constant

Correct Answer: B. Zero

Question 6

If Q₁ = 10 and Q₃ = 18, what is the interquartile range?

A. 8

B. 14

C. 28

D. 80

Correct Answer: A. 8

Question 7

Which statement about an outlier is correct?

A. It must always be deleted

B. It proves the data are invalid

C. It may be a genuine observation or a data error and requires investigation

D. It is always below the first quartile

Correct Answer: C. It may be a genuine observation or a data error and requires investigation

Question 8

Which practice improves accessibility?

A. Relying only on red and green

B. Hiding definitions in hover text only

C. Providing text alternatives and not relying on colour alone

D. Using the smallest possible labels

Correct Answer: C. Providing text alternatives and not relying on colour alone

Question 9

Why should a dashboard display denominators for rates?

A. To increase decorative detail

B. To show the population or exposure underlying the percentage

C. To eliminate all uncertainty

D. To convert rates into totals automatically

Correct Answer: B. To show the population or exposure underlying the percentage

Question 10

Which conclusion is justified by a high correlation between advertising and sales alone?

A. Advertising caused all observed sales

B. The variables have a strong linear association in the observed data

C. Sales will increase by exactly the same percentage as advertising

D. No third variable can be relevant

Correct Answer: B. The variables have a strong linear association in the observed data

Programming for Analytics
Chapter 9 of 14