6. Matplotlib#
This chapter moves from quick pandas plots to explicit Matplotlib control. You will learn the figure-and-axes model, common plotting commands, and the options that make charts reproducible and easier to adapt.
Figure and Axes: A figure is the whole canvas and each axes is one chart on it; most Matplotlib commands act on an axes.
Object-Oriented Plotting:
fig, ax = plt.subplots()keeps the figure and axes in variables, so multi-chart dashboards stay clear to build and adjust.Plot Types: Line, bar, scatter, histogram, and box plots each fit a different business question: trend, comparison, relationship, or distribution.
Chart Controls: Titles, labels, legends, colors, line styles, axis ranges, and shared axes turn a default plot into a chart a manager can read.
Saving Figures:
savefig()with a suitable size and dpi produces charts ready for reports and slides.
Chapter Video
Video credit: Schafer, C. (2019, June 10). Matplotlib Tutorial (Part 1): Creating and Customizing Our First Plots. YouTube. Watch on YouTube.
This embedded video is provided for educational reference only. All rights are retained by the respective copyright holder.
Learning Objectives
By the end of this chapter, you will be able to:
Distinguish a figure from its axes and build single- and multi-chart figures with
plt.subplots().Create line, bar, scatter, histogram, and box plots that match a management question about trend, comparison, relationship, or distribution.
Label and style charts with titles, axis labels, legends, colors, line styles, markers, transparency, and axis ranges.
Arrange charts for comparison with subplots, shared axes, figure titles, and
add_axes()insets.Set figure size and resolution, save figures for reports, and finish pandas plots with Matplotlib methods.
Chapter Flow
Glossary
# |
Term |
Explanation |
|---|---|---|
1 |
Matplotlib |
A Python library for creating charts; pandas and Seaborn plotting are built on it. |
2 |
pyplot |
The |
3 |
Stateful interface |
A pyplot style where functions such as |
4 |
Object-oriented interface |
A style that stores the figure and axes in variables and calls methods on them, such as |
5 |
Figure |
The whole canvas that holds one or more axes. |
6 |
Axes |
One chart inside a figure: the area where data, labels, and ticks are drawn. |
7 |
Axis |
The x or y scale of an axes, with its range, ticks, and label. |
8 |
Tick |
A marked reference value along an axis. |
9 |
|
A stateful function that adds one axes at a position in a grid. |
10 |
|
A function that creates a figure and its axes in one step and returns both. |
11 |
|
A function that adjusts spacing so subplot titles and labels do not overlap. |
12 |
Figure title |
One title above all subplots, set with |
13 |
Shared axis |
An axis range used by every subplot, set with |
14 |
|
A figure method that places an axes at |
15 |
Inset axes |
A small axes placed on top of a larger chart, often to zoom in on part of the data. |
16 |
Figure size |
The width and height of a figure in inches, set with |
17 |
DPI |
Dots per inch: the resolution used to draw or save a figure. |
18 |
|
A method that saves a figure to a file such as PNG, PDF, or SVG. |
19 |
Format string |
A short code such as |
20 |
Line style |
The pattern used to draw a line, such as solid, dashed, or dotted. |
21 |
Marker |
A symbol drawn at each data point, such as a circle or square. |
22 |
Alpha |
Transparency from 0 (invisible) to 1 (opaque), useful when marks overlap. |
23 |
Legend |
A key that explains which color or style belongs to which series. |
24 |
Title |
Text above an axes that names its subject or message. |
25 |
Axis label |
Text that says what an axis measures, including units. |
26 |
Axis range |
The lowest and highest values shown on an axis, set with |
27 |
rcParams |
Matplotlib’s default settings for every part of a chart (“runtime configuration”), loaded when Matplotlib starts and changeable while a program runs. |
28 |
Style sheet |
A named set of appearance settings applied with |
29 |
Line plot |
Points connected in order; suited to trends over time. |
30 |
Scatter plot |
Points for pairs of numeric values; suited to relationships. |
31 |
Bar plot |
Rectangles whose heights compare values across categories. |
32 |
Histogram |
Bars that count how many values fall in each bin; suited to distributions. |
33 |
Bin |
One interval of values counted in a histogram. |
34 |
Boxplot |
A summary of a distribution showing the median, quartiles, whiskers, and outliers. |