Chart types
Line plots, bar graphs, histograms, pie charts, frequency polygons, box plots and scatter plots.
The essentials
Matplotlib's pyplot module provides functions for many chart types. Each takes data for the x and/or y axis and draws the chart; plt.show() displays it. A Pandas Series or DataFrame can also be plotted with df.plot(kind=...), where kind can be 'line', 'bar', 'barh', 'hist', 'pie', 'box' or 'scatter'.
Key points
- Line plot: plt.plot(x, y). Shows change or trend over time, such as monthly temperature.
- Bar graph: plt.bar(x, height) draws vertical bars; plt.barh() draws horizontal bars. Compares quantities across categories, such as marks in subjects.
- Histogram: plt.hist(data, bins=5). Shows the distribution of a continuous variable by grouping values into intervals (bins); bars touch each other.
- Pie chart: plt.pie(values, labels=...). Shows the share of each part in a whole; autopct shows percentages.
- Frequency polygon: a line graph that joins the midpoints of the tops of the bars of a histogram. It shows the shape of a distribution and is useful for comparing two distributions.
- Box plot: plt.boxplot(data). Shows the five-number summary: minimum, first quartile, median, third quartile and maximum, and can show outliers as separate points.
- Scatter plot: plt.scatter(x, y). Shows the relationship (correlation) between two numeric variables, such as height and weight.
Exam tips
- Purpose decides the chart: trend → line; compare categories → bar; distribution → histogram; parts of a whole → pie; relationship → scatter; spread and outliers → box plot.
- In a histogram the x axis is a numeric range; in a bar chart it shows categories.
Common mistakes
- Using a pie chart for data that does not add up to a meaningful whole.
- Confusing a histogram with a bar chart because both use bars.
Practice questions
Log in to answer these questions and see the explanations.Create free accountLog in