Matplotlib
Matplotlib provides the basic charting tools used throughout much of the Python data ecosystem. Even once other, more specialised libraries come into play later, understanding Matplotlib remains genuinely valuable, since it teaches the underlying logic behind building any figure: define the data, create the figure, choose the chart type, label the axes, add a title, and display the result. That sequence barely changes regardless of which chart type you're building or which library eventually sits on top of it.
This section introduces three important chart families. Bar charts compare groups against each other, scatter plots explore relationships between two numerical variables, and histograms show how a single variable is distributed. Together, these three chart types cover a large proportion of the visualisation tasks that come up in everyday business analytics and applied research.
The examples below use sector-level sales, the relationship between employee count and sales, and the distribution of profit margins. These examples are intentionally kept simple, but they demonstrate a general workflow that adapts readily to many other business datasets.
Python Code: Bar Charts, Scatter Plots and Histograms
A bar chart: comparing sectors
# Several common chart types for business data
sector_sales = data.groupby("sector")["sales_million"].sum().sort_values()
plt.figure(figsize=(9, 5))
sector_sales.plot(kind="bar")
plt.title("Total Sales by Sector")
plt.xlabel("Sector")
plt.ylabel("Sales (£m)")
plt.xticks(rotation=45, ha="right")
plt.tight_layout()
plt.show()
A scatter plot: exploring a relationship
plt.figure(figsize=(8, 5))
plt.scatter(data["employees"], data["sales_million"])
plt.title("Sales and Number of Employees")
plt.xlabel("Employees")
plt.ylabel("Sales (£m)")
plt.grid(True)
plt.show()
A histogram: describing a distribution
plt.figure(figsize=(8, 5))
plt.hist(data["profit_margin"], bins=12)
plt.title("Distribution of Profit Margins")
plt.xlabel("Profit margin")
plt.ylabel("Frequency")
plt.grid(True)
plt.show()
Expected output / interpretation
The outputs should show: sector comparisons, a sales-employees relationship, and the distribution of profit margins.
Unpacking each chart in turn:
-
The bar chart first sorts sectors by total sales (
.sort_values()) before plotting, which is a small but meaningful choice: an unsorted bar chart forces the reader to hunt across the chart for the largest and smallest bars, whereas a sorted one presents the ranking immediately, at a glance.plt.xticks(rotation=45, ha="right")rotates the sector labels along the x-axis, a common and necessary adjustment whenever category names are too long to fit horizontally without overlapping.plt.tight_layout()then automatically adjusts the figure's spacing so that rotated labels, titles, and axes don't get visually clipped or cut off at the figure's edges. -
The scatter plot places
employeeson the x-axis andsales_millionon the y-axis, with each point representing a single company-year observation. Each individual dot corresponds to one row of the underlying dataset. Look for the overall shape of the point cloud: does it slope upward (larger firms tend to have higher sales), appear essentially flat (little relationship), or show a lot of scatter around a general trend (a relationship exists, but a fairly noisy one)? -
The histogram groups the
profit_marginvalues into 12 bins, equal-width intervals, and counts how many observations fall into each one. Thebinsparameter controls the resolution of this description: too few bins can hide meaningful structure in the data (like a second, smaller cluster of values), while too many bins can make the chart look overly jagged and noisy, dominated by random variation rather than genuine underlying patterns. Experimenting with a few different bin counts on the same data is a quick, worthwhile habit whenever a histogram's shape isn't immediately clear.
A recurring pattern worth noticing: all three examples follow the exact same underlying sequence, prepare the data (often with a groupby() or a direct column selection), create a figure, choose a plotting function (.plot(kind="bar"), plt.scatter(), or plt.hist()), add titles and axis labels, and finally call plt.show(). Once this rhythm feels familiar, adapting it to a new variable or a new chart type becomes largely a matter of substitution, swapping in different columns and a different plotting function, rather than learning an entirely new process each time.