Unit V · Basics of Python Programming for Pharmaceutical Sciences (BP101T) · As per PCI B.Pharmacy Syllabus, NEP 2020
A plot is only as useful as it is understandable, and a chart without labels forces the reader to guess what the numbers mean. In this part of Unit V, we look at how Matplotlib lets you annotate a figure with axis labels, a title, and a legend — the three elements that turn a raw line or scatter of points into a chart a pharmacist, examiner, or journal reviewer can interpret on sight. These annotation functions are simple to use, but examiners frequently test them because they reflect whether a student understands that a graph is a communication tool, not just a rendering of numbers.

Why Annotation Matters in Pharmaceutical Graphs
Every pharmaceutical dataset you plot — a concentration-time profile, a dissolution curve, a dose-response relationship — involves two or more physical quantities with specific units. A chart that simply shows a rising and falling line, with no indication of what is on the x-axis or y-axis, is scientifically meaningless. Regulatory submissions, research publications, and even routine laboratory reports require every axis to be labelled with its quantity and unit, every figure to carry a descriptive title, and every multi-series plot to carry a legend identifying each series. Matplotlib builds these requirements into three straightforward function calls: plt.xlabel(), plt.ylabel(), plt.title(), and plt.legend().
1. Labeling the Axes
The x-axis (horizontal) and y-axis (vertical) of a two-dimensional plot usually represent two different measured quantities. Matplotlib lets you attach a descriptive text string to each axis using plt.xlabel() and plt.ylabel(), so the viewer immediately understands what the values represent, without needing an accompanying paragraph of explanation.
Example: Labeling a Time-Temperature Plot
import matplotlib.pyplot as plt
import numpy as np
x = np.array([1, 2, 3, 4])
y = np.array([10, 20, 25, 30])
plt.plot(x, y)
plt.xlabel("Time (hours)")
plt.ylabel("Temperature (°C)")
plt.show()
Here, plt.xlabel("Time (hours)") places the text “Time (hours)” beneath the horizontal axis, and plt.ylabel("Temperature (°C)") places “Temperature (°C)” alongside the vertical axis. Running this code produces a line plot in which any reader, without asking a single question, knows exactly what is being measured on each axis.
Guidelines for Writing Good Axis Labels
A well-written axis label follows a few simple conventions that are worth memorising, since they apply to every plot you will ever produce in pharmaceutical data analysis:
- Always include the unit. Write “Time (hours)” rather than a bare “Time” — without the unit, a reader cannot tell if the values are seconds, minutes, or hours.
- Keep the wording short but informative. A label should communicate the measured quantity without turning into a sentence.
- Avoid vague wording. “Distance (km)” is unambiguous; a bare “Distance” leaves the unit to guesswork.
2. Adding a Title
A title sits above the plot and gives an immediate, one-line summary of what the entire figure is about. Rather than forcing the viewer to infer the purpose of a graph from its axes and data alone, a well-chosen title lets them grasp the point of the visualization at a glance. In Matplotlib, a title is added with a single function call, plt.title(), that takes the heading text you want displayed.
plt.title("Temperature Variation Over Time")
Adding this single line to the earlier example places the heading “Temperature Variation Over Time” directly above the plot, so anyone glancing at the figure instantly knows it illustrates how temperature changes as time passes. In pharmaceutical reporting, a good title often states both the drug/parameter and the condition being studied — for example, “Plasma Concentration of Paracetamol After Oral Administration” is far more informative than a generic “Concentration vs Time”.
3. Adding a Legend
When a single chart displays more than one dataset — for instance, drug concentration profiles for two different formulations, or temperature readings from two different cities — a legend is needed so the reader can tell the lines, markers, or colours apart. Creating a legend in Matplotlib is a two-step process: first, you assign a label to each plotted series as it is drawn, and then you call plt.legend() to actually render the legend box on the figure.
Example: Comparing Two Data Series with a Legend
import matplotlib.pyplot as plt
# Define data
x = [1, 2, 3, 4]
y_city_a = [10, 15, 20, 25]
y_city_b = [12, 18, 22, 28]
# Plot data with labels
plt.plot(x, y_city_a, label="City A")
plt.plot(x, y_city_b, label="City B")
# Labels and title
plt.xlabel("Time (hours)")
plt.ylabel("Temperature (°C)")
plt.title("Temperature Comparison")
# Show legend and graph
plt.legend()
plt.show()
In this code, the label argument passed to each plt.plot() call assigns a name (“City A” and “City B”) to that particular line. The axis labels and title are added exactly as before, and the call to plt.legend() then draws a small box on the chart mapping each line’s colour and marker style to its assigned label. The viewer can now instantly distinguish which trend belongs to which series — a feature that becomes essential the moment you plot, say, a test formulation against a reference formulation on the same axes.
xlabel(), ylabel(), title(), and, where more than one series is plotted, legend() with a label argument on each plot() call. Missing any one of these is a common reason for lost marks even when the plotting logic itself is correct.Putting It All Together
A properly annotated chart combines all three elements in a short, predictable sequence: draw the data, label the axes, add a title, and (if there are multiple series) add a legend. The order of these calls does not usually matter to Matplotlib, but writing them in a consistent sequence — plot, then xlabel, then ylabel, then title, then legend, then show — makes your code easier to read and easier to debug when a label does not appear as expected.
| Function | Purpose | Typical Pharmaceutical Use |
|---|---|---|
plt.xlabel() |
Labels the horizontal axis with text and unit | “Time (h)” on a concentration-time curve |
plt.ylabel() |
Labels the vertical axis with text and unit | “Plasma Concentration (mg/L)” |
plt.title() |
Adds a heading above the plot | “IV Bolus Concentration-Time Curve” |
label= (in plot()) + plt.legend() |
Names each series and displays a key | Distinguishing “Test Formulation” vs “Reference Formulation” |
Frequently Asked Questions
Do I always need to call plt.legend() if I use the label argument?
Yes. Passing label="City A" to plt.plot() only stores the name internally; it does not display anything on the chart by itself. You must call plt.legend() afterwards to actually draw the legend box. If you forget it, the chart will show the lines but no key to identify them.
Can I control where the legend appears on the plot?
Yes. plt.legend() accepts a loc parameter, for example plt.legend(loc="upper right") or plt.legend(loc="best"). The value "best" is the default in most Matplotlib versions and automatically places the legend where it overlaps the data least.
What happens if I add a title or axis label with special characters, like the degree symbol?
Matplotlib generally accepts any valid Python string, including special characters such as ° for degrees, provided your source file is saved with UTF-8 encoding (the default in most modern editors and IDEs). If a symbol renders incorrectly, check the file encoding first rather than the plotting code.
Summary
Axis labels, titles, and legends are the three annotation tools that convert a bare Matplotlib plot into a scientifically communicative figure. plt.xlabel() and plt.ylabel() state what each axis measures and in what unit; plt.title() summarises the figure’s purpose in one line; and the combination of the label argument with plt.legend() identifies multiple data series on the same chart. Mastering these four function calls is a small effort that pays off in every subsequent pharmaceutical visualization you build in this course, from dosage-response curves to dissolution profile comparisons.
References
- Pharmacy Council of India (PCI) – B.Pharm Regulations, NEP 2020, BP101T Syllabus (Basics of Python Programming for Pharmaceutical Sciences)
- Python Software Foundation – Official Python Documentation, docs.python.org
- Matplotlib Development Team – Official Matplotlib Documentation, matplotlib.org
- NumPy Developers – Official NumPy Documentation, numpy.org
