Module 25
Matplotlib
Draw clear line, bar, scatter, histogram, and pie charts with Matplotlib, arrange them in subplots, and save them as files.
01
What Is Matplotlib
Meet Matplotlib, the plotting library for Python, and draw a first chart from a list of numbers with pyplot in three short lines.
02
Anatomy of a Figure
Learn the parts of a Matplotlib chart: the figure that holds everything, the axes you draw on, and the title, labels, ticks, and legend.
03
Your First Line Plot
Draw line charts with plot, understand what happens when you pass only one list, and add the title and axis names that make a chart readable.
04
Multiple Lines and a Legend
Draw several lines on one set of axes, label each one, and place a legend so a reader can tell the lines apart without asking.
05
Line Styles and Colours
Control how a line looks with colour, line style, width, and transparency, and learn the short aliases that appear in most Matplotlib code.
06
Markers on a Line
Mark the real measurements on a line with marker, and control their shape, size, fill colour, and edge colour with ms, mfc, and mec.
07
Grid Lines and Fonts
Add grid lines so readers can trace a value back to the axis, and style the title and axis names with a font dictionary.
08
Axis Limits and Ticks
Zoom into part of a chart with xlim and ylim, choose which tick numbers appear with xticks and yticks, and see how limits can mislead.
09
Several Plots in One Figure
Split one figure into a grid with plt.subplot, give each panel its own title, and add one heading over the whole figure with suptitle.
10
Working with Figure and Axes Objects
Use plt.subplots to hold the figure and its axes in variables, and learn the set_ methods that replace the plt shortcuts on a real project.
11
Bar Charts
Compare separate categories with plt.bar, control colour and width, and understand why a bar chart must always start at zero.
12
Horizontal Bar Charts
Use plt.barh when category names are long, set the bar thickness with height, and order the bars so the chart reads from the top down.
13
Stacked Bar Charts
Stack one series on top of another with bottom, build the running total for a third layer, and know when stacking helps or hides.
14
Scatter Plots
Plot one dot per observation with plt.scatter to see whether two measurements move together, and read the association honestly.
15
Colour Maps and Dot Sizes
Add a third and fourth measurement to a scatter plot using the c and s arguments, a colour map, and a colour bar that explains the scale.
16
Histograms
Count how many values fall into each range with plt.hist, and learn why a histogram is a different chart from a bar chart.
17
Choosing Histogram Bins
Set the number of bins or their exact edges, work out the bin width, and see how the choice changes the shape a histogram shows.
18
Histogram Styles
Switch between the bar, step, and stepfilled histogram styles, thin the bars with rwidth, and set the fill and edge colours.
19
Pie Charts
Show how a whole splits into parts with plt.pie, label every slice, print the percentages with autopct, and know when a bar chart reads better.
20
Styling a Pie Chart
Pull one slice out with explode, rotate the pie with startangle, add a shadow or a donut hole, and put the labels in a legend instead.
21
Saving Figures
Save a finished chart with savefig as PNG, SVG, or PDF, set its size and resolution, trim the margins, and close figures you no longer need.