Programming for Data Science (Python/R)

Chapter 8
Data Visualization and Communication

Post Graduate Diploma in Data Science
Programming for Data Science (Python/R)   •   Post Graduate

8. Data Visualization and Communication

Introduction and Context

Data visualization is the disciplined use of graphical forms to explore, explain, and communicate data. In data science, visualization is not merely a final presentation activity. It supports data quality assessment, exploratory analysis, model diagnosis, decision-making, and organizational learning. A well-designed chart can reveal a trend, exception, relationship, or uncertainty that may remain hidden in a table. A poorly designed chart can create false impressions even when the underlying calculations are correct.

For data scientists, visualization therefore combines technical capability with statistical reasoning, visual perception, communication, and professional judgment. Python libraries such as Matplotlib, Seaborn, and Plotly, and the R package ggplot2, provide powerful ways to construct visual analyses. Dashboard platforms extend this work by allowing users to monitor indicators, filter data, and interact with results. However, tools do not determine whether a visualization is meaningful. That judgment depends on the question being asked, the characteristics of the data, the intended audience, and the decision that the visualization is expected to support.

This chapter develops a practical and analytical approach to visualization. It considers how people perceive visual encodings, how charts should be selected, how uncertainty should be represented, and how analytical findings can be transformed into executive reports and dashboards.

Learning Objectives

After completing this chapter, students should be able to:

1. Explain the role of visualization in exploratory analysis and communication.

2. Select appropriate charts for distributions, comparisons, relationships, trends, and spatial or hierarchical data.

3. Create visualizations using Matplotlib, Seaborn, Plotly, and ggplot2.

4. Apply principles of visual perception, accessibility, annotation, and truthful graphical representation.

5. Communicate uncertainty, variation, missingness, and data limitations responsibly.

6. Design dashboards and executive reports around decisions rather than around available charts.

7. Evaluate visualization choices in professional, managerial, ethical, and governance contexts.

Visualization as Analytical Reasoning

A visualization is an argument expressed through visual structure. The designer decides what to include, what to emphasize, what to aggregate, which comparisons to enable, and what context to provide. These choices influence how the audience interprets the evidence.

Visualization commonly serves two related purposes. Exploratory visualization is used by analysts to investigate data. It is often iterative, detailed, and provisional. Analysts may examine many plots, change scales, identify anomalies, and test alternative explanations. Explanatory visualization communicates a selected finding to an audience. It normally contains fewer elements, a clearer message, and more deliberate annotation.

The distinction is not absolute. A dashboard may support both exploration and explanation, but the interface should make its purpose clear. A senior manager reviewing a performance dashboard generally needs a reliable overview, important exceptions, and pathways to detail. An analyst investigating customer churn may need distributions, subgroup comparisons, model residuals, and interactive filtering.

A useful design sequence is:

1. Define the decision or question.

2. Identify the audience and its level of statistical knowledge.

3. Determine the analytical task: comparison, trend analysis, distribution analysis, relationship analysis, composition, or location.

4. Select variables and appropriate transformations.

5. Choose visual encodings and a chart form.

6. Inspect the visualization for accuracy, readability, accessibility, and possible misinterpretation.

7. Add annotations and uncertainty information.

8. Test the visualization with representative users and revise it.

This sequence prevents a common error: beginning with a visually attractive chart type and then searching for a question that it appears to answer.

Visual Perception and Graphical Encodings

A graphical encoding maps a data value to a visible property such as position, length, angle, area, colour, shape, or opacity. These properties are not equally easy to interpret. In general, position along a common scale and length are judged more accurately than angle, area, or colour intensity. This explains why a carefully designed bar chart is often more effective for comparing categories than a pie chart.

The following principles are especially important:

  • Position: Points located on a common axis support precise comparison. Scatter plots and aligned dot plots make strong use of position.
  • Length: Bars are effective when their baseline and scale are clear.
  • Colour: Hue can distinguish categories, while lightness or saturation can indicate ordered magnitude. Colour should not be the only way to communicate an important distinction.
  • Area and volume: Bubbles, circles, and three-dimensional objects are difficult to compare accurately and should be used cautiously.
  • Shape: Symbols can distinguish categories but are less suitable for representing fine quantitative differences.
  • Connection: Lines and arrows suggest continuity, direction, or sequence, but connecting unrelated categories may imply a relationship that does not exist.

Visual hierarchy determines where attention goes first. Size, contrast, location, whitespace, and colour can direct attention to an important measure or exception. A chart should establish a clear hierarchy rather than giving every element equal visual prominence.

A chart also has a data-ink ratio, meaning the proportion of visible graphical elements that convey data rather than decoration. The concept is useful, but it should not be applied mechanically. Gridlines, labels, and reference bands can improve interpretation. The goal is not minimal decoration at any cost; it is purposeful design.

Scales require particular care. A truncated bar-chart axis can exaggerate small differences because viewers judge bars by their apparent lengths. For standard bar charts, a zero baseline is usually appropriate. Line charts may use a narrower vertical range when the objective is to show variation over time, provided the scale is clearly labelled and the visual message is not misleading. Logarithmic scales are valuable for data spanning several orders of magnitude, but they must be explained because equal distances represent ratios rather than equal absolute differences.

Choosing Charts for Analytical Tasks

Chart selection should follow the structure of the question.

Comparisons and rankings

Bar charts are suitable for comparing a modest number of categories. Horizontal bars are often preferable when category names are long. A dot plot can provide a cleaner alternative when many categories must be ranked. Sorting categories by value usually improves interpretation, unless the natural order is meaningful, such as months or process stages.

Trends over time

Line charts are appropriate when observations have an ordered temporal structure and connected points represent continuity. A line chart should not connect arbitrary categories simply because they can be placed on an axis. Multiple lines should be limited or carefully highlighted; excessive series create visual clutter. Small multiples can show the same trend separately for regions, products, or customer segments while preserving a common scale.

Distributions

Histograms show the frequency of values within intervals, but the choice of bin width affects the apparent shape. Density plots provide a smoothed representation and are useful for comparing groups, although smoothing can conceal local features. Box plots summarize median, quartiles, and potential outliers. Violin plots combine a density shape with summary information but may be less familiar to non-technical audiences. A distribution should not be reduced to its mean when skewness, multiple modes, or extreme values matter.

Relationships

Scatter plots are useful for examining association between two quantitative variables. A fitted trend line can summarize a pattern, but it does not establish causation. Colour, shape, or facets can represent a third variable, though too many encodings may make interpretation difficult. Correlation should be interpreted alongside domain knowledge, possible confounding variables, and the form of the relationship.

Composition

Stacked bars can show how a total is divided, especially when the number of components is small and a common baseline exists. 100-percent stacked bars emphasize proportions rather than totals. Pie charts are acceptable only for simple part-to-whole comparisons with few categories; they are generally weaker than aligned bars for precise comparison. Composition over time is often better shown with carefully designed stacked areas or separate lines, depending on whether totals and component trends are both important.

Hierarchy, networks, and geography

Treemaps can display hierarchical composition when space efficiency is important, but the areas are difficult to compare precisely. Network diagrams are appropriate when connections are themselves the subject, but they can become unreadable as the number of nodes grows. Maps are useful when geographic location is analytically relevant. A choropleth map should normally encode a rate, proportion, or other normalized measure rather than a raw count, since larger regions may otherwise appear more important merely because they contain more people.

The Python and R Visualization Ecosystem

Matplotlib

Matplotlib is a foundational Python library offering detailed control over figures, axes, scales, annotations, legends, and export formats. It is well suited to publication-quality static charts and customized analytical graphics.

```python

import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(8, 4.5))

ax.plot(months, revenue, marker="o", color="#24527a")

ax.set_title("Monthly revenue")

ax.set_xlabel("Month")

ax.set_ylabel("Revenue ($ thousands)")

ax.grid(axis="y", alpha=0.25)

fig.tight_layout()

plt.show()

```

The object-oriented interface, which explicitly creates a figure and axes, is preferable for reusable code and multi-panel layouts. Analysts should label units, use meaningful titles, and avoid relying on default colours or unexplained abbreviations.

Seaborn

Seaborn builds on Matplotlib and provides a higher-level interface for statistical graphics. It integrates naturally with tabular data and offers functions for categorical comparisons, distributions, regression views, and faceting.

```python

import seaborn as sns

import matplotlib.pyplot as plt

sns.set_theme(style="whitegrid")

ax = sns.boxplot(data=df, x="segment", y="delivery_days", color="#8ecae6")

ax.set(title="Delivery time by customer segment",

xlabel="Customer segment", ylabel="Days")

plt.tight_layout()

```

Seaborn's `hue` and faceting features are powerful, but the number of groups should remain manageable. Statistical summaries generated by a library must still be checked: an automatically displayed confidence interval is not necessarily the interval most appropriate for the decision.

Plotly

Plotly supports interactive charts with hover information, zooming, filtering, and browser-based delivery. Interactivity is valuable when users need to inspect individual observations or move between overview and detail. It should not be used to conceal a confusing static design. A chart that only makes sense after extensive hovering has weak explanatory power.

```python

import plotly.express as px

fig = px.scatter(df, x="ad_spend", y="sales", color="region",

hover_data=["product"],

title="Sales and advertising expenditure")

fig.update_layout(template="simple_white")

fig.show()

```

Interactive charts should have accessible labels, sensible default views, and clear controls. Performance also matters: very large datasets may require aggregation, sampling, WebGL rendering, or server-side filtering.

ggplot2

ggplot2, widely used in R, is based on the grammar of graphics. A graphic is constructed from data, aesthetic mappings, geometric objects, statistical transformations, scales, coordinate systems, and facets. This layered approach separates the meaning of a chart from its visual appearance.

```r

library(ggplot2)

ggplot(df, aes(x = segment, y = delivery_days, fill = segment)) +

geom_boxplot(show.legend = FALSE) +

labs(title = "Delivery time by customer segment",

x = "Customer segment", y = "Days") +

theme_minimal()

```

The grammar encourages reproducibility because the data transformation and visual mappings are explicit. It also makes it easier to create consistent small multiples and themes for organizational reporting.

Communicating Uncertainty and Data Quality

A visualization that displays an estimate without its uncertainty can imply more precision than the data justify. Uncertainty may arise from sampling variation, measurement error, missing observations, model assumptions, forecasting, or changing definitions.

Common representations include error bars, confidence or credible intervals, prediction intervals, shaded bands, distributions of simulation results, and annotated ranges. These forms are not interchangeable. A confidence interval for a mean describes uncertainty about an estimated parameter under a specified procedure; a prediction interval concerns the likely range of a future individual observation and is usually wider. The chart caption should identify what the interval represents.

Uncertainty should also be communicated when comparing groups. If two point estimates differ, the visual should not automatically imply a meaningful difference. Analysts may report intervals, effect sizes, practical thresholds, or decision-relevant probabilities. Statistical significance alone does not establish managerial importance.

Data quality should be visible where it affects interpretation. Missing values can be shown with gaps, explicit categories, or notes. Suppressed values, small sample sizes, revised historical records, and changes in measurement definitions should be documented. Aggregation may conceal variation and inequality; a dashboard showing an overall average should provide a route to subgroup analysis when fairness or operational risk is relevant.

From Charts to Dashboards and Executive Reports

A dashboard is an information system, not simply a collection of charts. Its design begins with users, decisions, refresh frequency, data ownership, and action thresholds. A useful dashboard answers questions such as: What is happening? Where is performance outside expectations? Why might it be happening? What action is available?

A practical dashboard architecture often includes:

  • a small set of headline indicators with definitions and comparison values;
  • trend views showing direction and context;
  • diagnostic views supporting segmentation or drill-down;
  • alerts based on explicit thresholds or statistical rules;
  • metadata describing source, refresh time, ownership, and limitations.

Indicators should be defined consistently. A metric such as customer retention may vary depending on the cohort, observation period, and treatment of reactivated customers. Without a data dictionary, visually polished dashboards can encourage inconsistent decisions.

Executive reporting requires compression without distortion. Senior audiences usually need the decision implication first, followed by evidence and limitations. A strong report may contain a headline stating the finding, one primary chart, a short explanation of drivers, and a recommended action. It should distinguish observed facts from interpretation and recommendation. Decorative dashboards with many gauges, saturated colours, or unnecessary three-dimensional effects often increase cognitive load rather than insight.

Accessibility, Ethics, and Governance

Accessible visualization is a professional obligation. Designers should use adequate contrast, readable font sizes, descriptive titles, direct labels where practical, and colour palettes that remain interpretable for people with colour-vision deficiencies. Patterns, symbols, line styles, and text should supplement colour. Keyboard navigation, alternative text, and screen-reader-compatible descriptions are important in digital products.

Ethically, visualization can mislead through selective time windows, inappropriate denominators, suppressed baselines, unequal group definitions, or selective annotation. The issue is not only whether a chart is technically accurate, but also whether a reasonable viewer is likely to form a materially false impression.

Privacy must be considered when charts reveal individual-level information, small cells, locations, or combinations of attributes. Aggregation and suppression may be required. Governance practices should identify data provenance, transformation steps, metric ownership, version history, access permissions, and retention requirements. A reproducible script or notebook should accompany important reports so that the chart can be audited and regenerated.

Fairness is also relevant. A model-performance dashboard that reports only overall accuracy may conceal poor performance for a protected or operationally important subgroup. Disaggregated views, sample-size information, and uncertainty estimates can support more responsible evaluation.

An Illustrative Case: Reducing Delivery Delays

Background

A fictional online retailer, Northstar Home, receives increasing complaints about late deliveries. The operations director asks the data science team to prepare a dashboard and recommend interventions. The available data include order date, promised date, actual delivery date, warehouse, carrier, product category, destination region, and customer type.

Situation and analytical problem

The initial report shows that the average delivery time is 3.8 days, close to the target of four days. Management is inclined to conclude that performance is satisfactory. The data scientist first defines lateness as actual delivery date later than promised delivery date and separates average delivery time from the proportion of late orders.

Analysis

A histogram reveals a right-skewed distribution: most orders arrive within three days, but a small group is delayed substantially. A bar chart of late-order rates by warehouse identifies two warehouses with materially higher rates. A weekly line chart shows that delays increased after a carrier change. A box plot by carrier indicates greater variation for the new carrier, while a faceted chart shows that the problem is concentrated in bulky products and remote regions.

The analyst includes sample sizes and intervals for late-order rates. One warehouse has a high observed rate but few orders, so its estimate is uncertain. The dashboard distinguishes the overall rate, the rate for bulky products, and the rate for remote destinations. It does not use a red-green colour scheme alone; icons and labels identify status categories.

Approach and solution

The executive page contains three indicators: late-order rate, median delay among late orders, and the number of affected orders. A trend chart provides a comparison with the previous period. A diagnostic page allows managers to filter by warehouse, carrier, product category, and region. The data dictionary defines each metric, and a note explains that delivery dates are recorded in local time.

The operations team pilots a revised carrier allocation rule for bulky products in remote regions. The dashboard is used weekly to compare the pilot group with similar orders, while analysts monitor both average outcomes and variability.

Outcome

The organization does not judge the pilot solely by the overall mean. It examines the late-order rate, the distribution of delays, customer complaints, and operational cost. This prevents an improvement in one segment from being mistaken for universal improvement. The case illustrates how visualization connects measurement, diagnosis, action, and evaluation.

Learning points

The most important insight was not produced by a single sophisticated chart. It emerged from defining the metric carefully, examining distributions and subgroups, displaying uncertainty, and designing the dashboard around an operational decision. A headline average would have hidden the concentration of the problem.

Common Failure Modes and Better Practice

Several errors recur in professional visualization. Choosing a chart before defining the question often produces irrelevant decoration. Excessive use of colour, labels, or interactive controls can obscure the main message. Dual axes may encourage readers to infer a relationship between unrelated scales. A pie chart with many slices makes comparison difficult. A map may attract attention even when geography is not analytically relevant. Overplotting can hide dense observations, while excessive smoothing can manufacture an apparent pattern.

Other mistakes are less visible. Analysts may compare percentages calculated from different denominators, mix nominal and real monetary values, ignore seasonality, or report an average without showing its distribution. Automated defaults may select inappropriate confidence intervals or order categories alphabetically when a meaningful order exists. A dashboard can also fail operationally if its data refresh is delayed, its metric definitions change, or no person is responsible for responding to alerts.

Best practice is iterative. Begin with a rough chart, inspect it analytically, obtain feedback from intended users, test accessibility, and document assumptions. Use consistent scales when comparing panels, but do not force a common scale when it makes important variation invisible; explain the choice. Prefer direct labels and informative titles. Export charts in formats that preserve resolution and maintain a reproducible source version.

Current Developments

Visualization is increasingly integrated with cloud data platforms, notebook environments, business-intelligence systems, and automated reporting pipelines. Declarative specifications allow a chart to be rendered across multiple technologies while preserving its analytical meaning. Real-time dashboards support monitoring of operations, but they also increase the risk of alert fatigue and unstable metrics.

Natural-language interfaces and generative AI can assist with chart suggestions, code generation, narrative summaries, and anomaly detection. Their outputs require human review because an automated system may select an unsuitable aggregation, misunderstand a denominator, or express a causal interpretation that the data do not support. Emerging practice therefore emphasizes provenance, explainability, permission controls, and human approval for high-impact communications.

Chapter Summary

Data visualization is a form of analytical reasoning as well as communication. Effective practice begins with a decision or question, identifies the audience, and selects graphical encodings that make relevant comparisons perceptually clear. Position and length generally support more accurate comparison than area, angle, or volume. Bar charts, line charts, distribution plots, scatter plots, maps, and hierarchical displays each serve different analytical tasks.

Matplotlib and Seaborn support flexible Python graphics, Plotly adds interactivity, and ggplot2 provides a layered grammar for reproducible R visualization. Good visualization communicates uncertainty, variation, missingness, and data definitions rather than presenting estimates as unquestionable facts. Dashboards should be designed around decisions, users, refresh processes, and actions. Accessibility, privacy, fairness, reproducibility, and governance are essential professional responsibilities. The strongest executive communication combines a clear message, well-chosen evidence, relevant context, and an honest account of limitations.

Key Terms

Data visualization: The graphical representation of data to support exploration, explanation, and decision-making.

Exploratory visualization: Provisional graphical analysis used to discover patterns, anomalies, and questions.

Explanatory visualization: A deliberately designed graphic intended to communicate a selected finding.

Graphical encoding: The mapping of data values to visual properties such as position, length, colour, or shape.

Visual hierarchy: The arrangement of visual emphasis that guides the viewer's attention.

Data-ink ratio: The proportion of visible graphical elements that directly communicate data.

Small multiples: A set of related charts using consistent structure to compare groups or conditions.

Histogram: A chart showing the frequency of quantitative values within intervals.

Box plot: A summary display based on quartiles, median, and potential outliers.

Scatter plot: A chart showing the relationship between two quantitative variables.

Choropleth map: A map that shades geographic areas according to a measured value.

Faceting: Dividing a visualization into panels according to one or more categorical variables.

Confidence interval: An interval produced by a statistical procedure to express uncertainty about an estimated parameter.

Prediction interval: An interval describing plausible values for a future or individual observation.

Dashboard: An interactive information display that combines indicators, charts, filters, and contextual information.

Data dictionary: Documentation defining data fields, metrics, units, and calculation rules.

Accessibility: The design of information so that people with varied abilities and technologies can use and interpret it.

Overplotting: The visual overlap of marks that conceals observations or density.

Reproducibility: The ability to regenerate an analysis and its visual outputs from documented data and code.

Visual deception: A design choice that creates a materially misleading impression despite, or sometimes through, technically correct data.

Review Questions

Short Answer Questions

1. Distinguish exploratory visualization from explanatory visualization.

2. Why is position generally a stronger quantitative encoding than area?

3. What analytical purpose does a box plot serve?

4. State two reasons why uncertainty should be displayed in a chart.

5. What information should a dashboard data dictionary contain?

Long Answer Questions

1. Explain how visual perception influences the selection of bar charts, pie charts, bubble charts, and scatter plots.

2. Compare Matplotlib, Seaborn, Plotly, and ggplot2 in terms of purpose, flexibility, and interactivity.

3. Describe a systematic process for designing an executive dashboard.

4. Discuss the ethical and governance risks associated with misleading or poorly documented visualizations.

5. Explain how distributions, subgroup analysis, and uncertainty can improve interpretation beyond reporting an overall average.

Analytical / Application Questions

1. A manager reports that average delivery time is within target, but customer complaints have increased. Design an analytical visualization sequence to investigate the apparent contradiction and justify each chart.

2. A company dashboard uses red and green status colours, a truncated y-axis, and no sample sizes for subgroup rates. Evaluate the design and propose specific improvements.

3. You are asked to visualize sales across regions with very different population sizes. Explain whether raw counts, rates, or both should be used, and describe how you would communicate the limitations of the comparison.

22. Multiple Choice Questions with Answers

Question 1

A data scientist wants to compare the sales of eight product categories accurately. Which chart is generally most appropriate?

A. A bar chart with a common baseline

B. A three-dimensional pie chart

C. A bubble chart with different areas

D. A decorative word cloud

Correct Answer: A. A bar chart with a common baseline

Question 2

Which visualization is most appropriate for examining the distribution and potential outliers of delivery times across customer segments?

A. A box plot

B. A choropleth map

C. A stacked area chart

D. A network diagram

Correct Answer: A. A box plot

Question 3

Why can a truncated baseline be problematic in a bar chart?

A. It may exaggerate differences in bar lengths

B. It always prevents the use of labels

C. It converts categorical data into continuous data

D. It removes all uncertainty from the data

Correct Answer: A. It may exaggerate differences in bar lengths

Question 4

Which statement best distinguishes a prediction interval from a confidence interval?

A. A prediction interval concerns plausible future observations, while a confidence interval concerns an estimated parameter

B. A prediction interval is always narrower than a confidence interval

C. A confidence interval is used only for categorical variables

D. The two terms always describe exactly the same quantity

Correct Answer: A. A prediction interval concerns plausible future observations, while a confidence interval concerns an estimated parameter

Question 5

A map compares the number of hospital admissions in regions with very different populations. What is the primary analytical concern?

A. Raw counts may reflect population size rather than admission prevalence

B. Maps cannot display quantitative information

C. A map automatically creates a causal relationship

D. Population size is irrelevant to geographic comparisons

Correct Answer: A. Raw counts may reflect population size rather than admission prevalence

Question 6

Which feature is most characteristic of the grammar of graphics used by ggplot2?

A. Charts are built from layers such as data, aesthetics, geometric objects, scales, and facets

B. Only interactive browser charts can be created

C. All charts must use three-dimensional coordinates

D. Charts cannot include statistical transformations

Correct Answer: A. Charts are built from layers such as data, aesthetics, geometric objects, scales, and facets

Question 7

An executive dashboard contains twenty indicators but gives no definitions, refresh time, or data owner. Which improvement is most important for governance?

A. Add metric definitions, provenance, refresh information, and ownership

B. Replace all charts with pie charts

C. Remove all explanatory text

D. Use more saturated colours

Correct Answer: A. Add metric definitions, provenance, refresh information, and ownership

Question 8

A scatter plot shows a strong association between advertising expenditure and sales. What is the most responsible interpretation?

A. The association should be examined alongside confounding factors before claiming causation

B. Advertising expenditure must be the only cause of sales

C. The relationship proves that every product will respond identically

D. Correlation makes uncertainty unnecessary

Correct Answer: A. The association should be examined alongside confounding factors before claiming causation

Question 9

Which design choice best improves accessibility when colour communicates categories?

A. Use labels, symbols, or line styles in addition to colour

B. Use more similar colours so differences are subtle

C. Remove all category labels

D. Use colour as the only distinguishing feature

Correct Answer: A. Use labels, symbols, or line styles in addition to colour

Question 10

In the Northstar Home case, why was the overall average delivery time insufficient?

A. It concealed a skewed distribution and delays concentrated in particular segments

B. Averages cannot be calculated from delivery data

C. The dashboard contained too few colours

D. The data included more than one warehouse

Correct Answer: A. It concealed a skewed distribution and delays concentrated in particular segments

Programming for Data Science (Python/R)
Chapter 8 of 15