Programming for Data Processing

Lectures: Nguyễn Tuấn Phong, Đào Việt Anh

Labs: Trịnh Ngọc Huỳnh, Phạm Tiến Du

Semester: 2025-2026-1

Institute for AI, VNU-UET

Cute panda drawing illustration

Lecture 9
Data Visualization with Python

Today

  • Tools: Matplotlib, Seaborn, pandas.plot, Plotly Express, Altair
  • Goal: clear, honest, useful charts
  • Demo: Link to Google Colab Notebook (use VNU account to access)

1.
Introduction

Why Visualize?

  • See structure; Spot anomalies; Communicate
  • Tasks → charts:
    • Compare → bar / point / line
    • Distribution → hist / KDE / ECDF / box / violin
    • Relationship → scatter / line (+ trend)
    • Composition → stacked bars/area (sparingly)
  • Start with a question, not a chart

Design Quick Hits

  • Position > Length > Angle/Area/Color: Clear, honest comparisons
  • Label units; Add reference lines
  • Colorblind-safe palettes; minimize colors
  • Use log scale for heavy tails
  • Avoid truncated axes without context

2.
Matplotlib

Matplotlib Mental Model

Object-oriented (OO) API

  • Figure → Axes → Artists
  • OO pattern: fig, ax = plt.subplots() → methods on ax
  • Explicit; Reusable; Composable
  • Save: fig.savefig(...)

Matplotlib Mental Model

Object-oriented (OO) API

Matplotlib Techniques

  • Subplots: fig, axs = plt.subplots(r, c, sharex=True)
  • Legends/ticks/grid/annotations via ax methods
  • Avoid misleading dual y-axes → normalize or small multiples
  • Vector export: SVG/PDF for publication-ready articles

3.
Seaborn

Seaborn: What & Why

  • High-level stats plots on Matplotlib
  • Axes-level (e.g., sns.boxplot)
  • Figure-level (e.g., displot, relplot)
  • Theme once: sns.set_theme(style="whitegrid", palette="colorblind")

Distributions: KDE vs ECDF

  • KDE: smoothed shape; risk of over-smoothing
  • ECDF: exact cumulative distribution
  • Use both for group comparison (e.g., penguins)
  • Order categories by statistic (e.g., median)

Distributions: KDE vs ECDF

Relationships & Categoricals

  • Trend/uncertainty: sns.lmplot, sns.regplot
  • Overplotting: use alpha, jitter, size
  • Summaries: sns.pointplot(estimator="median", errorbar=("pi", 50))
  • Keep OO: pass ax= when using axes-level funcs

Faceting

  • FacetGrid(..., col="island", hue="species")
  • map_dataframe(sns.scatterplot, x=..., y=...)
  • Shared scales → fair comparisons
  • Labels & titles: g.set_axis_labels(...).set_titles("{col_name}")

Faceting

4.
Direct Plotting with pandas

Quick EDA with pandas.plot

  • Fast first pass from DataFrame/Series
  • Timeseries: rolling mean, resample
  • Then refine in Seaborn/Matplotlib
  • Example: df.plot(kind="line"), df.hist()

5.
Interactive Visualization

Interactivity: Plotly Express

  • Hover / Zoom / Facet in one line
  • Trendline: trendline="ols" (needs statsmodels)
  • Share: fig.write_html("figure.html")

Altair: Declarative Grammar

  • Map fields → encodings (concise)
  • Faceting via row/column
  • Tooltips by default; HTML export

6.
Best Practices & Pitfalls

Choosing the Right Tool

  • pandas.plot → quickest EDA
  • Matplotlib → full control, publication quality
  • Seaborn → statistical defaults + faceting
  • Plotly/Altair → interactivity & shareable HTML
  • Pattern: explore → refine → share

Reproducibility & Pitfalls

  • Seed randomness; Pin data versions
  • Consistent styles across plots
  • Common pitfalls:
    • Dual y-axes without care
    • Missing units/labels
    • Rainbow palettes
    • Truncated axes without context
    • Over-smoothed KDE