Force 0 tick to appear in a scatter plot

5 views (last 30 days)
I cannot make the 0 tick appear nomatter what I tried. Here is my code.
clc, clear, clf
n = 0:10; f = n.^2;
scatter(n,f, 'k', 'filled')
xlim([-1,11]), ylim([-10,120])
ax = gca;
ax.XAxisLocation = 'origin'; ax.YAxisLocation = 'origin';
ax.XTick = -2:2:10; ax.YTick = -20:20:120;
xlabel('n'), ylabel('f(n)=n^2')
title('Sequence f(n)=n^2')
grid on; grid minor
For the sake of clarity, I wanted the ticks in the origin to appear explicitly as in the following figure (the rest of the plot is irrelevant).

Accepted Answer

Les Beckham
Les Beckham on 26 Jan 2023
clc, clear, clf
n = 0:10; f = n.^2;
scatter(n,f, 'k', 'filled')
xlim([-1,11]), ylim([-10,120])
ax = gca;
ax.XAxisLocation = 'origin'; ax.YAxisLocation = 'origin';
ax.XTick = -2:2:10; ax.YTick = -20:20:120;
xlabel('n'), ylabel('f(n)=n^2')
title('Sequence f(n)=n^2')
grid on; grid minor
ax.XTickLabels = -2:2:10;
  3 Comments
Les Beckham
Les Beckham on 26 Jan 2023
Edited: Les Beckham on 26 Jan 2023
You are quite welcome.
ax.XTick sets the values of the axis at which you wish to have tick marks. ax.XTickLabel sets the strings (or char vectors) to be used as labels for those tick marks.
BTW - the title of your questions says "stem plot" but this is a scatter. Maybe you could change the title to avoid confusion for people reading this (or searching) in the future?
Dimitrios Anagnostou
Dimitrios Anagnostou on 26 Jan 2023
Very useful comment. Thanks again. I did the correction!

Sign in to comment.

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!