Plotting Data Using Matplotlib
Section B2, Informatics Practices. Covers Chart types; Customising plots.
About this unit
Charts make patterns in data easy to see. This unit uses the pyplot module of Matplotlib, and the plot() method of Pandas, to draw line plots, bar graphs, histograms, pie charts, frequency polygons, box plots and scatter plots. It also shows how to customise a chart with colours, line styles, markers, labels, titles and legends, and how to save it as an image file.
Key ideas
- import matplotlib.pyplot as plt; draw with a function, then plt.show() or plt.savefig().
- plot() for line charts, bar() and barh() for bar charts, hist() for histograms, pie() for pie charts, scatter() for scatter plots and boxplot() for box plots.
- df.plot(kind='bar') and similar calls plot DataFrame columns directly.
- Customisation: color, linestyle, linewidth, marker, xlabel, ylabel, title, legend and grid.
How to prepare
Learn which chart suits which purpose: trend, comparison, distribution, share of a whole, or relationship. Memorise the function names and common arguments, and practise reading the code of a small plot to say what it will display.
Topics in this chapter
Chart types
Line plots, bar graphs, histograms, pie charts, frequency polygons, box plots and scatter plots.
5 questionsCustomising plots
Colour, line style and width, labels, titles and legends; saving plots.
5 questions