Big picture
A table of numbers hides patterns that a chart reveals instantly: rising sales, which city has the most rain, how marks are spread. Data visualisation is the graphical representation of data, and in Python it is done with the Matplotlib library, especially its pyplot module.
The board paper asks you to write or complete plotting code, choose the right chart type, and understand histogram bins. Learn the function names and their key arguments.
Learning goals
Explain the purpose of data visualisation.
Plot line charts, bar charts and histograms with
pyplot.Customise plots with titles, labels, legends, colours, line styles, markers, ticks and grids.
Explain how histogram bins work and count values in each bin.
Save plots and plot data directly from Pandas.
Core ideas
1. Why visualise data?
To understand trends, patterns and relationships quickly.
To compare values across categories.
To spot outliers and the spread of data.
To communicate findings clearly to others.
2. Getting started
import matplotlib.pyplot as plt
x = [2019, 2020, 2021, 2022, 2023]
y = [120, 90, 150, 170, 200]
plt.plot(x, y)
plt.show()plt.show() displays the figure; plt.savefig('sales.png') saves it to a file (call savefig() before show()).
3. Line chart
A line chart joins data points with straight lines. It is best for showing trends over time (continuous data).
plt.plot(x, y, color='red', linestyle='--', marker='o',
linewidth=2, markersize=8, label='Sales')
plt.title('Yearly Sales')
plt.xlabel('Year')
plt.ylabel('Units sold')
plt.legend()
plt.grid(True)
plt.show()Customisation options:
Argument or function | Purpose | Examples |
|---|---|---|
| Line colour |
|
| Line style |
|
| Thickness of the line |
|
| Symbol at each data point |
|
| Marker size and edge colour | |
| Name of the series shown in the legend |
|
| Chart title and axis labels | |
| Positions and labels of ticks |
|
| Shows grid lines |
Several lines on one chart: call plt.plot() several times before plt.show(), each with its own label, and add plt.legend().
4. Bar chart
A bar chart uses rectangular bars to compare values across categories.
cities = ['Delhi', 'Mumbai', 'Pune', 'Agra']
rain = [70, 240, 120, 60]
plt.bar(cities, rain, color='skyblue', width=0.5)
plt.title('Monthly Rainfall')
plt.xlabel('City')
plt.ylabel('Rainfall (mm)')
plt.show()plt.barh()draws horizontal bars (useheightinstead ofwidth).widthsets bar thickness (default 0.8).Multiple bar chart: shift the positions of the second set of bars so they sit beside the first.
import numpy as np
idx = np.arange(len(cities))
plt.bar(idx, rain_2023, width=0.4, label='2023')
plt.bar(idx + 0.4, rain_2024, width=0.4, label='2024')
plt.xticks(idx + 0.2, cities)
plt.legend()
plt.show()5. Histogram
A histogram shows the distribution of numerical data by grouping values into ranges called bins and drawing a bar for the count in each bin. Unlike a bar chart, the bars touch, because the bins cover a continuous range.
marks = [10, 12, 15, 22, 25, 28, 31, 35, 40, 45, 48]
plt.hist(marks, bins=[10, 20, 30, 40, 50], color='orange', edgecolor='black')
plt.show()How bins work: each bin includes its left edge but not its right edge, except the last bin, which includes both edges.
Bin | Values | Count |
|---|---|---|
10–20 | 10, 12, 15 | 3 |
20–30 | 22, 25, 28 | 3 |
30–40 | 31, 35 | 2 |
40–50 (50 included) | 40, 45, 48 | 3 |
binscan be a number (Matplotlib makes that many equal-width bins between the minimum and maximum) or a list of edges.Other arguments:
histtype('bar','step','stepfilled'),cumulative=True,orientation='horizontal',edgecolor.
6. Bar chart and histogram compared
Bar chart | Histogram |
|---|---|
Compares categories (cities, subjects) | Shows the distribution of a numerical variable |
Gaps between bars | No gaps; bars touch |
Bars can be reordered | Bins follow numerical order |
|
|
7. Plotting from Pandas
A DataFrame can plot itself using Matplotlib behind the scenes:
import pandas as pd
df = pd.DataFrame({'Year': [2021, 2022, 2023],
'Sales': [150, 170, 200],
'Profit': [30, 45, 60]})
df.plot(x='Year', y=['Sales', 'Profit'], kind='line', title='Performance')
plt.show()
df.plot(x='Year', y='Sales', kind='bar')
df['Sales'].plot(kind='hist', bins=3)kind can be 'line', 'bar', 'barh', 'hist', 'box', 'area', 'pie' or 'scatter'.
A reliable routine for plotting questions
Choose the chart: trend over time (line), compare categories (bar), distribution of values (histogram).
import matplotlib.pyplot as plt, then call the plotting function with x and y (or data and bins).Add title, axis labels, and a legend if there are several series.
Call
savefig()(if required) and thenshow().For histogram counts, apply the bin rule: left edge included, right edge excluded, except the last bin.
Connections that matter
Data handling with Pandas prepares the data that is plotted.
Economics: organisation and presentation of data explains bar diagrams and histograms in statistics.
Data communication project uses charts to present findings.
Common mistakes
Using a histogram to compare categories, or a bar chart for a distribution.
Calling `plt.legend()` without giving any `label`.
Calling `savefig()` after `show()`, which may save a blank image.
Placing a value equal to a bin's right edge in that bin (except the last bin).
Forgetting `import matplotlib.pyplot as plt`.
Revision checklist
I can explain the purpose of visualisation.
I can draw and customise line charts.
I can draw vertical, horizontal and multiple bar charts.
I can draw histograms and count values in bins.
I can save figures and plot directly from Pandas.