0
0
Matplotlibdata~5 mins

Title and axis labels in Matplotlib

Choose your learning style9 modes available
Introduction

Adding a title and labels to the axes helps people understand what the chart shows. It makes the graph clear and easy to read.

When you want to explain what your chart is about.
When sharing graphs with others who did not create them.
When you want to highlight what data is on the x-axis and y-axis.
When preparing reports or presentations with charts.
When comparing multiple charts and need clear descriptions.
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 plot.

Use plt.xlabel() and plt.ylabel() to name the horizontal and vertical axes.

Examples
This adds a title and labels to a sales chart showing months on the x-axis and sales in dollars on the y-axis.
Matplotlib
plt.title('Sales Over Time')
plt.xlabel('Month')
plt.ylabel('Sales ($)')
This labels a temperature chart with time on the x-axis and temperature in degrees Celsius on the y-axis.
Matplotlib
plt.title('Temperature vs Time')
plt.xlabel('Time (hours)')
plt.ylabel('Temperature (°C)')
Sample Program

This program creates a simple line chart with x and y values. It adds a title and labels to the axes so the chart is easy to understand.

Matplotlib
import matplotlib.pyplot as plt

# Sample data
x = [1, 2, 3, 4, 5]
y = [10, 20, 25, 30, 40]

plt.plot(x, y)
plt.title('Example Line Chart')
plt.xlabel('X values')
plt.ylabel('Y values')
plt.show()
OutputSuccess
Important Notes

You can customize the font size and color by adding parameters like fontsize=14 or color='red' inside the label functions.

Always add axis labels and titles to make your charts clear and professional.

Summary

Use plt.title() to add a chart title.

Use plt.xlabel() and plt.ylabel() to label the axes.

Clear titles and labels help others understand your charts easily.