Add y grid only to plot

328 次查看(过去 30 天)
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.
  13 个评论
dpb
dpb 2016-7-14
"gca.YGrid='on';is not valid syntax to do this."
I debated about that; not having HG2 wasn't able to check; I (unfortunately) let the previous posting influence in making an unwarranted assumption so left it instead of using the old set notation trying to "get with it" for the new stuff... :(
At least finally uncovered the issue; as I suspected it wasn't profound.
chlor thanks
chlor thanks 2016-7-14
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!!

请先登录,再进行评论。

采纳的回答

Mathieu
Mathieu 2016-7-13
Hello,
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 个评论
chlor thanks
chlor thanks 2016-7-13
That is smart, if I can't figure things out I will have to give it a try, thanks for sharing :D

请先登录,再进行评论。

更多回答(1 个)

Valerii Abramenko
Valerii Abramenko 2019-7-23
Hello,
If someone else needs the grid for only one axis, the answer is here (set(gca, 'YGrid', 'on', 'XGrid', 'off')):
  3 个评论
Klont
Klont 2024-2-9
What about minor gridlines for only one axis?
grid(gca,'minor')
displays them for both X and Y, ignoring
set(gca,'XGrid','on','YGrid','off')
Francesco
Francesco 2024-2-19
编辑:Francesco 2024-2-19
set(gca, 'XMinorGrid', 'on', 'YMinorGrid', 'off')

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Specifying Target for Graphics Output 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by