0
0
MATLABdata~5 mins

Labels, title, and legend in MATLAB

Choose your learning style9 modes available
Introduction

Labels, titles, and legends help explain what a graph shows. They make charts easier to understand.

When you want to explain what the x-axis and y-axis represent in a plot.
When you want to give a clear title to your graph to show its main idea.
When you have multiple lines or data sets and want to identify each one with a legend.
When sharing graphs with others to make the information clear and easy to read.
When preparing reports or presentations that include charts.
Syntax
MATLAB
xlabel('x-axis label')
ylabel('y-axis label')
title('Graph title')
legend('label1', 'label2', ...)

Use xlabel and ylabel to name the axes.

title adds a heading to the plot.

legend shows labels for multiple data series.

Examples
This adds labels to the x and y axes and a title to the plot.
MATLAB
xlabel('Time (seconds)')
ylabel('Speed (m/s)')
title('Speed over Time')
This adds a legend to identify two lines on the same plot.
MATLAB
plot(x1, y1, x2, y2)
legend('Car A', 'Car B')
Labels only the x-axis and adds a title.
MATLAB
xlabel('Temperature (°C)')
title('Temperature Changes')
Sample Program

This program plots sine and cosine curves on the same graph. It labels the x-axis as 'Angle (radians)', the y-axis as 'Value', adds a title, and a legend to distinguish the two lines.

MATLAB
x = 0:0.1:2*pi;
y1 = sin(x);
y2 = cos(x);
plot(x, y1, '-r', x, y2, '-b')
xlabel('Angle (radians)')
ylabel('Value')
title('Sine and Cosine Functions')
legend('sin(x)', 'cos(x)')
OutputSuccess
Important Notes

Always add labels and titles to make your plots clear.

Use legends when you have more than one data series in a plot.

You can customize font size and style using additional properties if needed.

Summary

Labels name the axes to explain what data they show.

Titles give the graph a clear heading.

Legends identify multiple data sets on the same plot.