Matplotlib and seaborn Fundamentals
Explore how to use Matplotlib and Seaborn to build effective data visualizations that answer key questions through clear charts. Learn to prepare tidy data, choose appropriate chart types, and add essential elements like titles, axis labels with units, and legends. Understand how to interpret line charts, bar plots, scatter plots, histograms, and box plots to reveal trends, compare groups, and analyze distributions accurately for better data storytelling.
A chart can contain correct data and still fail its audience when on_time_rate has no percent unit, 95 has no label, and two lines have no legend. With the framing checks already in place, we can use a repeatable plotting workflow that makes the message readable.
Start with a tidy pandas DataFrame, where each column has one clear meaning and each row is an observation. Choose the chart type that matches the question, then map columns to visible marks such as points, lines, bars, or positions. Finally, make the chart usable with a specific title, axis labels that include units, a legend when more than one series appears, sensible axis limits, and colors that distinguish groups without becoming the only way to read the data.
Matplotlib is a Python plotting library that gives direct control over chart elements such as lines, labels, legends, and limits. seaborn is a higher-level plotting interface that works naturally with pandas DataFrames and creates common exploratory charts with less code. seaborn builds on Matplotlib, so we can use seaborn to create a chart and Matplotlib functions to finish it.
Finishing check: Before sharing a chart, check that its title states the subject, each axis names the measure and unit, and every color or line has an understandable label.
A readable delivery-rate line chart
A line chart answers a trend question, so the logistics chart below shows daily on-time delivery rate from 2025-04-01 through 2025-04-14. The target_rate series makes the operational standard visible instead of forcing readers to remember that the target is 95%.
Lines 5–29: The code builds a tidy 14-row DataFrame for 2025-04-01 through 2025-04-14 with
on_time_ratestored as percentage values and a constanttarget_rateof 95, matching the lesson’s “rates already expressed as percentages” contract.Lines 32–40: The code plots two series with explicit labels and distinct styling (solid vs. dashed) so the legend separates “Actual rate” from “Target rate” without relying on color alone. ...