Back to Python Notes
Topic #272

Labels and Titles

Learn how to add descriptive labels and titles to matplotlib plots to make data visualizations clear and professional.

What it is

In data visualization, raw numbers are often meaningless without context. Labels and titles provide this context by identifying what is being measured (axes) and summarizing the plot's purpose (title). In Python's matplotlib.pyplot library, these elements are added using specific functions that modify the current active figure or axes object.

The mental model is simple: think of your plot as a canvas. The xlabel and ylabel functions write text along the bottom and left edges, respectively, while title places text at the top center. Related terms include "legend" (which identifies multiple data series) and "tick labels" (the small numbers on the axes themselves).

Why it matters

  • Clarity: Viewers can instantly understand what units and variables are represented.
  • Professionalism: Unlabeled charts look like debugging output; labeled charts look like finished products.
  • Accessibility: Proper labeling helps screen readers and users with visual impairments interpret the graph structure.
  • Context: A title explains the "so what?" of the data, guiding the viewer’s interpretation.

Syntax or steps

The basic syntax involves calling three functions after plotting your data but before displaying the figure:

  1. plt.xlabel("text"): Sets the label for the horizontal axis.
  2. plt.ylabel("text"): Sets the label for the vertical axis.
  3. plt.title("text"): Sets the main title of the chart.

You can also pass keyword arguments like fontsize, color, or fontweight to customize appearance.

Example

import matplotlib.pyplot as plt

# Sample data
months = ["Jan", "Feb", "Mar", "Apr"]
sales = [150, 200, 180, 220]

# Create the plot
plt.plot(months, sales, marker='o')

# Add annotations
plt.xlabel("Month")
plt.ylabel("Sales (in thousands)")
plt.title("Monthly Sales Performance Q1-Q2")

# Display the plot
plt.show()

Explanation: First, we import matplotlib.pyplot. We define lists for months and sales figures. The plt.plot function creates the line graph. Crucially, we then call plt.xlabel and plt.ylabel to describe the axes. Finally, plt.title adds a header. Without these lines, the viewer would not know if "150" represents dollars, units, or temperature.

Common mistakes

  • Forgetting to call plt.show(): If you run the script in an interactive environment without showing the plot, changes might not render immediately.
  • Using generic labels: Avoid labels like "Value" or "X". Always specify units (e.g., "Temperature (°C)").
  • Overlapping text: Long titles or labels may overlap with tick marks. Use plt.tight_layout() before plt.show() to automatically adjust spacing.
  • Confusing title with suptitle: plt.title applies to the current subplot. If you have multiple subplots and want one big header for the whole figure, use plt.suptitle.

When to use it

Compare standard labeling with using LaTeX formatting for complex mathematical symbols.

MethodBest ForComplexity
plt.xlabel("Text")Standard business/science chartsLow
plt.xlabel(r"$\alpha$")Mathematical notation/Greek lettersMedium

Use standard strings for most cases. Use raw strings (r"...") with LaTeX syntax only when you need special characters like Greek letters or fractions.

Practice

Guided Exercise: Plot the squares of numbers 1 through 5. Label the x-axis "Number", y-axis "Square Value", and title it "Quadratic Growth".

Challenge: Modify the previous exercise to change the title font size to 14 and color to blue. Hint: Use fontsize=14, color='blue' inside the plt.title function.

Quick check

Question: Which function sets the text at the top center of a single subplot?

Answer: plt.title()

Summary

Labels and titles transform raw data plots into communicative tools. By consistently applying xlabel, ylabel, and title, you ensure your audience understands exactly what they are looking at, regardless of their technical background.

Want to go beyond the notes?

Join Coding Now Tech Institute's Python course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Labels and Titles – FAQs

Quick answers about learning Labels and Titles in Python.

This free note from Coding Now Tech Institute explains Labels and Titles in Python — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Python topic on Coding Now Tech Institute, including Labels and Titles, is 100% free with no signup required.
With focused practice, most students grasp Labels and Titles in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now