0
0
Matplotlibdata~5 mins

Label, title, and axis names in Matplotlib

Choose your learning style9 modes available
Introduction

Labels, titles, and axis names help explain what a chart shows. They make graphs easy to understand.

When you want to explain what data your graph shows.
When sharing charts with others who need clear information.
When comparing different data sets and you want to name each axis.
When making reports or presentations with graphs.
When you want to add a title to summarize the graph's message.
Syntax
Matplotlib
plt.title('Your Title')
plt.xlabel('X-axis Label')
plt.ylabel('Y-axis Label')
Use plt.title() to add a main title to the graph.
Use plt.xlabel() and plt.ylabel() to name the horizontal and vertical axes.
Examples
This adds a title and labels for a sales chart showing months and sales amounts.
Matplotlib
plt.title('Sales Over Time')
plt.xlabel('Month')
plt.ylabel('Sales ($)')
Labels the axes with units to clarify what the numbers mean.
Matplotlib
plt.title('Temperature vs Time')
plt.xlabel('Time (hours)')
plt.ylabel('Temperature (°C)')
You can leave the title empty if you only want axis labels.
Matplotlib
plt.title('')
plt.xlabel('Age')
plt.ylabel('Height (cm)')
Sample Program

This code plots sales data over four months. It adds a title and labels the axes to explain the data clearly.

Matplotlib
import matplotlib.pyplot as plt

# Sample data
months = ['Jan', 'Feb', 'Mar', 'Apr']
sales = [250, 300, 400, 350]

plt.plot(months, sales)
plt.title('Monthly Sales')
plt.xlabel('Month')
plt.ylabel('Sales in USD')
plt.show()
OutputSuccess
Important Notes

Always add labels and titles to make your charts easy to read.

You can customize font size and style by adding parameters like fontsize inside the label and title functions.

If you forget labels, your graph might confuse people who see it.

Summary

Labels and titles explain what your graph shows.

Use plt.title(), plt.xlabel(), and plt.ylabel() to add them.

Clear labels help others understand your data quickly.