How can I change the interval on the y-axis?

786 views (last 30 days)
Hello everyone,
how can I change the interval on the y-axis?
For istance, if I want to put an interval every 25 or 50.
Thank you.

Accepted Answer

Scott MacKenzie
Scott MacKenzie on 16 Aug 2021
Edited: Scott MacKenzie on 16 Aug 2021
Here's some example code on how to control the "ticks":
x = 1:5;
y = randi(100, 1, 5);
set(gca, 'ylim', [0 100]);
set(gca, 'ytick', 0:5:100);
set(gca, 'ylim', [0 100]);
set(gca, 'ytick', 0:25:100);
set(gca, 'ylim', [0 100]);
set(gca, 'ytick', 0:20:100);
  1 Comment
Pul on 16 Aug 2021
Yes, I tried, but I got the result you can see in the picture.
But the scale is wrong, because the value 30, for cyan and magenta data, should be lower and not at the end.

Sign in to comment.

More Answers (2)

Pul on 16 Aug 2021
set(gca, 'ylim', [0 1400]);
set(gca, 'ytick', 0:25:1400);
Yeah, I know.
I tried in this way (every 25). But if I don't want to have labels so close but just some labels every 25?
Pul on 16 Aug 2021
Oh yes, I'm going to answer in the right box (see you above).

Sign in to comment.

Steven Lord
Steven Lord on 16 Aug 2021
You can use the yticks and yticklabels functions to control the locations of the ticks and the labels used for those ticks.
x = 0:0.5:32;
y = x.^2;
plot(x, y)
Let's make a copy of that plot (so you can see the difference when I run the code in MATLAB Answers) and update the tick locations.
plot(x, y)
Let's say I want to put ticks every 50 units but only to label multiples of 100.
plot(x, y)
ticklocations = 0:50:1050;
% Create the tick labels from the tick locations
% Or you could use
% ticklabels = yticklabels;
ticklabels = string(ticklocations);
% Replace the labels for any tick location that's not a multiple of 100
% with a blank label
ticklabels(mod(ticklocations, 100) ~= 0) = "";

Community Treasure Hunt

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

Start Hunting!