How to set the grid line of y-axis by 5 ?

3 vues (au cours des 30 derniers jours)
Rahim Rahim
Rahim Rahim le 23 Juil 2022
I want to add a grid on my plot. Unofrtunetely, I found only Grid normal and Grid miror,
I want to add a grid that appear every 5 value on y-axis, for example:

Réponses (2)

Voss le 23 Juil 2022
Maybe something like this?
ax = gca();
set(ax, ...
'XLim',[0.7 4.5], ...
'YLim',[0 40], ...
'XTick',1:4, ...
'YTick',0:5:40, ...
'GridColor','r', ...
'XGrid','on', ...
ytl = get(ax,'YTickLabels');
ytl(2:2:end) = {''};

Star Strider
Star Strider le 23 Juil 2022
If you have R2018b or later, the easiest solution would likely be the xline and yline functions.
x = 0:5;
y = randi(40, size(x));
plot(x, y)

Community Treasure Hunt

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

Start Hunting!

Translated by