Matplotlib

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:

  1. Distinguish a figure from its axes and build single- and multi-chart figures with plt.subplots().

  2. Create line, bar, scatter, histogram, and box plots that match a management question about trend, comparison, relationship, or distribution.

  3. Label and style charts with titles, axis labels, legends, colors, line styles, markers, transparency, and axis ranges.

  4. Arrange charts for comparison with subplots, shared axes, figure titles, and add_axes() insets.

  5. 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 matplotlib.pyplot module, usually imported as plt, that provides the plotting functions.

3

Stateful interface

A pyplot style where functions such as plt.plot() and plt.title() act on the current figure and axes.

4

Object-oriented interface

A style that stores the figure and axes in variables and calls methods on them, such as ax.set_title().

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

plt.subplot()

A stateful function that adds one axes at a position in a grid.

10

plt.subplots()

A function that creates a figure and its axes in one step and returns both.

11

tight_layout()

A function that adjusts spacing so subplot titles and labels do not overlap.

12

Figure title

One title above all subplots, set with fig.suptitle().

13

Shared axis

An axis range used by every subplot, set with sharex=True or sharey=True, so charts compare fairly.

14

add_axes()

A figure method that places an axes at [left, bottom, width, height] in fractions of the figure.

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 figsize=.

17

DPI

Dots per inch: the resolution used to draw or save a figure.

18

savefig()

A method that saves a figure to a file such as PNG, PDF, or SVG.

19

Format string

A short code such as 'r--' that sets color, line style, and marker in one argument.

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 set_xlim() and set_ylim().

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 plt.style.use().

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.

Chapter Overview Slides