Bird
Raised Fist0
MATLABdata~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the MATLAB command axis([0 10 0 5]) do to a plot?
easy
A. Sets the x-axis limits from 0 to 10 and y-axis limits from 0 to 5
B. Sets the axis color to RGB values [0 10 0 5]
C. Creates a grid with 10 rows and 5 columns
D. Labels the axes with numbers 0, 10, 0, and 5

Solution

  1. Step 1: Understand the axis function input

    The axis function with a vector of four numbers sets axis limits: [xmin xmax ymin ymax].
  2. Step 2: Apply the given limits

    Here, [0 10 0 5] means x-axis from 0 to 10 and y-axis from 0 to 5.
  3. Final Answer:

    Sets the x-axis limits from 0 to 10 and y-axis limits from 0 to 5 -> Option A
  4. Quick Check:

    axis([xmin xmax ymin ymax]) = set axis limits [OK]
Hint: Remember axis([xmin xmax ymin ymax]) sets axis limits [OK]
Common Mistakes:
  • Confusing axis limits with colors
  • Thinking axis sets labels or grid size
  • Using axis without four numbers
2. Which of the following is the correct syntax to set the x-axis label to 'Time (s)' in MATLAB?
easy
A. labelx('Time (s)')
B. xlabel('Time (s)')
C. setxlabel('Time (s)')
D. xaxislabel('Time (s)')

Solution

  1. Step 1: Recall MATLAB function for x-axis label

    The function to set x-axis label is xlabel with the label text as input.
  2. Step 2: Check syntax correctness

    xlabel('Time (s)') correctly sets the label. Other options are invalid function names.
  3. Final Answer:

    xlabel('Time (s)') -> Option B
  4. Quick Check:

    Use xlabel('text') to label x-axis [OK]
Hint: Use xlabel('text') exactly to label x-axis [OK]
Common Mistakes:
  • Using wrong function names like setxlabel
  • Forgetting quotes around label text
  • Confusing xlabel with axis or title
3. What will be the effect of the following MATLAB code?
plot(1:5, [2 4 6 8 10]);
axis([0 6 0 12]);
xticks([0 2 4 6]);
yticks([0 6 12]);
medium
A. Plot points with x-axis from 0 to 6, y-axis from 0 to 12, x-ticks at 0,2,4,6 and y-ticks at 0,6,12
B. Plot points with default axis limits and ticks
C. Error because xticks and yticks require vectors of length 5
D. Plot with reversed axes limits

Solution

  1. Step 1: Analyze axis limits

    axis([0 6 0 12]) sets x-axis from 0 to 6 and y-axis from 0 to 12.
  2. Step 2: Analyze tick settings

    xticks([0 2 4 6]) sets x-axis ticks at 0, 2, 4, 6; yticks([0 6 12]) sets y-axis ticks at 0, 6, 12.
  3. Final Answer:

    Plot points with x-axis from 0 to 6, y-axis from 0 to 12, x-ticks at 0,2,4,6 and y-ticks at 0,6,12 -> Option A
  4. Quick Check:

    axis + xticks + yticks control limits and ticks [OK]
Hint: axis sets limits; xticks/yticks set tick marks [OK]
Common Mistakes:
  • Assuming default ticks remain after setting axis
  • Thinking xticks/yticks need same length as data
  • Confusing axis limits with tick positions
4. Identify the error in this MATLAB code snippet for setting axis limits and fix it:
plot(1:3, [5 10 15])
axis(0 3 0 20)
medium
A. Limits must be strings; use axis('0 3 0 20')
B. axis function does not set limits; use xlim and ylim instead
C. Missing square brackets around limits; should be axis([0 3 0 20])
D. No error; code runs fine

Solution

  1. Step 1: Check axis function syntax

    The axis function requires a vector input for limits, enclosed in square brackets.
  2. Step 2: Identify missing brackets

    The code uses axis(0 3 0 20) without brackets, causing syntax error.
  3. Final Answer:

    Missing square brackets around limits; should be axis([0 3 0 20]) -> Option C
  4. Quick Check:

    axis([xmin xmax ymin ymax]) needs brackets [OK]
Hint: Always use square brackets for axis limits [OK]
Common Mistakes:
  • Omitting brackets around axis limits
  • Using strings instead of numeric vectors
  • Confusing axis with xlim or ylim functions
5. You want to create a plot with x-axis from -5 to 5, y-axis from -10 to 10, and custom ticks every 2 units on both axes. Which MATLAB code correctly achieves this?
hard
A. plot(x,y); axis([-5 5 -10 10]); xticks([-5 2 5]); yticks([-10 2 10]);
B. plot(x,y); axis([-5 5 -10 10]); xticks(-5:5); yticks(-10:10);
C. plot(x,y); xlim([-5 5]); ylim([-10 10]); ticks([-5 5], [-10 10]);
D. plot(x,y); axis([-5 5 -10 10]); xticks(-5:2:5); yticks(-10:2:10);

Solution

  1. Step 1: Set axis limits correctly

    axis([-5 5 -10 10]) sets x-axis and y-axis limits as required.
  2. Step 2: Set ticks every 2 units

    xticks(-5:2:5) creates ticks at -5, -3, -1, 1, 3, 5; similarly for y-axis with yticks(-10:2:10).
  3. Step 3: Check other options

    plot(x,y); axis([-5 5 -10 10]); xticks([-5 2 5]); yticks([-10 2 10]); sets ticks incorrectly (missing intermediate values). plot(x,y); xlim([-5 5]); ylim([-10 10]); ticks([-5 5], [-10 10]); uses invalid ticks function. plot(x,y); axis([-5 5 -10 10]); xticks(-5:5); yticks(-10:10); sets ticks every 5 units, not 2.
  4. Final Answer:

    plot(x,y); axis([-5 5 -10 10]); xticks(-5:2:5); yticks(-10:2:10); -> Option D
  5. Quick Check:

    Use axis for limits and colon operator for ticks [OK]
Hint: Use colon operator for ticks: start:step:end [OK]
Common Mistakes:
  • Setting ticks with wrong intervals
  • Using non-existent ticks() function
  • Confusing axis limits with tick values
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard