Matplotlib – Plot Labels

Matplotlib – Plot Labels

In Matplotlib, you can enhance the clarity of your plot by adding labels to the X and Y axes. Labels provide context and information about the data being visualized.

The X-axis label can be specified using xlabel() function.

The Y-axis label can be specified using ylabel() function.

For example, in the following program, we shall plot a line graph, and then specify X and Y axes labels.

Let us specify the X-axis label as 'X-axis Sample Label', and the Y-axis label as 'Y-axis Sample Label'. Based on you your requirement, you may specify other label values to the X and Y axes.

Python Program

import matplotlib.pyplot as plt

# Example data
x = [1, 2, 3, 4, 5]
y = [10, 20, 35, 45, 30]

# Plot the data
plt.plot(x, y, marker='o')

# Add labels to the X and Y axes
plt.xlabel('X-axis Sample Label')
plt.ylabel('Y-axis Sample Label')

# Show the plot
plt.show()

Output

Matplotlib - Plot Labels

Code copied to clipboard successfully 👍