0
0
MATLABdata~10 mins

Axis control and formatting in MATLAB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Axis control and formatting
Create Plot
Set Axis Limits
Format Axis Ticks
Add Labels and Title
Display Formatted Plot
This flow shows how to create a plot, set axis limits, format ticks, add labels, and display the final formatted plot.
Execution Sample
MATLAB
x = 0:0.1:2*pi;
y = sin(x);
plot(x,y)
xlim([0 2*pi])
ylim([-1 1])
xticks([0 pi 2*pi])
xticklabels({'0','\pi','2\pi'})
xlabel('Angle (radians)')
ylabel('sin(x)')
title('Sine Wave')
This code plots a sine wave, sets axis limits, customizes tick marks and labels, and adds axis labels and a title.
Execution Table
StepActionVariable/PropertyValue/EffectOutput/Plot Change
1Create x vectorx[0 0.1 0.2 ... 6.2 6.3]x data ready
2Calculate y = sin(x)y[0 0.0998 0.1987 ... -0.0998 -0]y data ready
3Plot x vs yplotLine plot of sine wavePlot appears with default axes
4Set x-axis limitsxlim[0 6.2832]x-axis limited from 0 to 2*pi
5Set y-axis limitsylim[-1 1]y-axis limited from -1 to 1
6Set x-axis ticksxticks[0 3.1416 6.2832]Ticks at 0, pi, 2pi
7Set x-axis tick labelsxticklabels['0', '\pi', '2\pi']Tick labels changed to 0, π, 2π
8Add x-axis labelxlabel'Angle (radians)'x-axis labeled
9Add y-axis labelylabel'sin(x)'y-axis labeled
10Add plot titletitle'Sine Wave'Title displayed
11End--Plot fully formatted and displayed
💡 All formatting commands applied; plot displayed with custom axis limits, ticks, labels, and title.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
xundefined[0 0.1 ... 6.3][0 0.1 ... 6.3][0 0.1 ... 6.3]
yundefinedundefined[0 0.0998 ... -0][0 0.0998 ... -0]
xlimdefaultdefaultdefault[0 6.2832]
ylimdefaultdefaultdefault[-1 1]
xticksdefaultdefaultdefault[0 3.1416 6.2832]
xticklabelsdefaultdefaultdefault['0', '\pi', '2\pi']
Key Moments - 3 Insights
Why do the x-axis tick labels show '0', '\pi', '2\pi' instead of numeric values?
Because in step 7, xticklabels replaces numeric tick labels with the specified strings, so the axis shows symbolic labels instead of numbers.
What happens if xlim is set after plotting?
As shown in step 4, setting xlim after plotting changes the visible range of the x-axis without re-plotting the data.
Why is ylim set to [-1 1] when sine values already lie in that range?
Setting ylim explicitly (step 5) ensures the y-axis limits are fixed and consistent, avoiding automatic scaling that might add extra space.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 6. What are the x-axis tick values set to?
A[0 3.1416 6.2832]
B[0 1 2]
CDefault numeric ticks
D['0', '\pi', '2\pi']
💡 Hint
Check the 'Value/Effect' column at step 6 in the execution_table.
At which step does the plot get its title?
AStep 9
BStep 10
CStep 8
DStep 11
💡 Hint
Look for the 'Add plot title' action in the execution_table.
If you remove the xticklabels command, what will the x-axis labels show?
ANo labels
BDefault labels '0', '\pi', '2\pi'
CNumeric values at ticks
DError in plot
💡 Hint
Refer to variable_tracker and note that xticklabels changes labels from numeric to strings.
Concept Snapshot
Axis control and formatting in MATLAB:
- Use xlim([min max]) and ylim([min max]) to set axis limits.
- Use xticks([...]) and yticks([...]) to set tick positions.
- Use xticklabels({...}) and yticklabels({...}) to set custom tick labels.
- Add labels with xlabel('text'), ylabel('text'), and title('text').
- Apply formatting after plotting to customize appearance.
Full Transcript
This example shows how MATLAB controls and formats plot axes step-by-step. First, it creates x and y data for a sine wave. Then it plots the data. Next, it sets the x-axis limits from 0 to 2*pi and y-axis limits from -1 to 1 to focus the view. After that, it customizes the x-axis ticks to appear at 0, pi, and 2*pi, and replaces their numeric labels with symbolic strings '0', 'π', and '2π'. Finally, it adds labels to the x and y axes and a title to the plot. The execution table traces each step, showing how variables and properties change. Key moments clarify why labels change and how axis limits affect the plot. The quiz tests understanding of tick values, title placement, and label defaults. This visual trace helps beginners see exactly how MATLAB commands shape the plot's axes and appearance.