0
0
MATLABdata~10 mins

Labels, title, and legend in MATLAB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Labels, title, and legend
Create plot data
Plot data on graph
Add x-axis label
Add y-axis label
Add title
Add legend
Display complete graph
This flow shows how to create a plot, then add labels for x and y axes, a title, and a legend step-by-step.
Execution Sample
MATLAB
x = 1:5;
y = x.^2;
plot(x,y)
xlabel('X values')
ylabel('Y values')
title('Square Numbers')
legend('y = x^2')
This code plots y = x squared, then adds labels to the axes, a title, and a legend.
Execution Table
StepActionEffect on PlotOutput/Display
1Create x and y data arraysData ready for plottingNo visible output
2Plot x vs yGraph with points/line appearsGraph window shows curve
3Add xlabel 'X values'X-axis labeledX-axis shows label 'X values'
4Add ylabel 'Y values'Y-axis labeledY-axis shows label 'Y values'
5Add title 'Square Numbers'Title added on topTitle 'Square Numbers' appears above plot
6Add legend 'y = x^2'Legend box appearsLegend box shows 'y = x^2'
7Display complete graphAll labels, title, legend visibleFinal plot with all annotations
💡 All labels, title, and legend added; plot fully annotated and displayed
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 7
xundefined[1 2 3 4 5][1 2 3 4 5][1 2 3 4 5]
yundefined[1 4 9 16 25][1 4 9 16 25][1 4 9 16 25]
Key Moments - 3 Insights
Why do we add xlabel and ylabel after plotting the data?
Because the plot must exist first to know where to place the labels; see execution_table steps 2 and 3.
What happens if we forget to add a legend?
The plot will show the data but no description of what the line or points represent; see execution_table step 6.
Can we add multiple legends for multiple lines?
Yes, by passing multiple strings to legend() matching each plotted line; this example shows one legend for one line.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what label is added at step 4?
AY-axis label
BX-axis label
CTitle
DLegend
💡 Hint
Check the 'Action' column at step 4 in execution_table
At which step does the plot first become visible?
AStep 1
BStep 2
CStep 3
DStep 6
💡 Hint
Look at 'Effect on Plot' column to see when graph appears
If we remove the legend command, what changes in the final plot?
ANo title will show
BNo axis labels will show
CNo legend box will appear
DPlot will not display
💡 Hint
Refer to execution_table step 6 and 7 about legend presence
Concept Snapshot
plot(x,y) - draws the graph
xlabel('text') - labels x-axis
ylabel('text') - labels y-axis
title('text') - adds title
legend('label') - adds legend box
Add these after plotting for clear graphs.
Full Transcript
This example shows how to plot data in MATLAB and add labels, title, and legend. First, we create x and y data arrays. Then we plot y versus x. After the plot appears, we add an x-axis label with xlabel, a y-axis label with ylabel, a title with title, and a legend with legend. Each step updates the graph visually. The legend describes the plotted data line. Adding these elements helps make the graph easy to understand. The execution table traces each step and its effect on the plot. The variable tracker shows how x and y values remain the same after creation. Key moments clarify why labels come after plotting and the importance of the legend. The quiz tests understanding of when labels appear and what happens if the legend is missing.