Add y grid only to plot

514 views (last 30 days)
chlor thanks
chlor thanks on 12 Jul 2016
Answered: Valerii Abramenko on 23 Jul 2019
I would like to add solid y grid lines to my plot according to the y tick in gca.
so I tried:
ax = gca;
ax.YGrid = 'on';
ax.GridLineStyle = '-';
However, this did not work and there is no grid at all in my plot, please help.
chlor thanks
chlor thanks on 14 Jul 2016
Everything works wonderfully after Adam points out what is wrong with my code.
It turns out that if I want to change the gridline color without affecting the tick color in my 2011 matlab, I need to manually draw those lines instead of using YGrid, so another type of fun for me... Just a little side story.
But still I learnt a lot from your comments, thank you guys!!

Sign in to comment.

Accepted Answer

Mathieu on 13 Jul 2016
I did something like this in my current project : I create my own lines to simulate grid like I want (and set the HitTest property to off to not take care of these lines with matlab cursor for example) through multiple plots. It is not very elegant but it is working very well and permit you to change as you want the position of the grid.
  1 Comment
chlor thanks
chlor thanks on 13 Jul 2016
That is smart, if I can't figure things out I will have to give it a try, thanks for sharing :D

Sign in to comment.

More Answers (1)

Valerii Abramenko
Valerii Abramenko on 23 Jul 2019
If someone else needs the grid for only one axis, the answer is here (set(gca, 'YGrid', 'on', 'XGrid', 'off')):

Community Treasure Hunt

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

Start Hunting!

Translated by