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

Adam
Adam 2016년 7월 12일
This shows a grid when I try it. Are you sure you have YTick marks defined?
The grid shows grey, but you can change its colour and other properties to make it stand out more
chlor thanks
chlor thanks 2016년 7월 12일
편집: chlor thanks 2016년 7월 12일
I believe so, this is what I have for defining YTick..
set(gca, 'XTick', x, 'YTick',[-0.01, 0, 0.01])
ax = gca;
ax.YGrid = 'on';
ax.GridLineStyle = '-';
and after double checking, I do not have even a hint of grid anywhere at all in my plot.
dpb
dpb 2016년 7월 12일
What does
ylim
return? Sounds like you've got the grid values outside those of the axes y limits.
If that doesn't answer the question, give us a complete sample that causes the problem we can run w/o making anything up for comparison.
chlor thanks
chlor thanks 2016년 7월 12일
편집: chlor thanks 2016년 7월 14일
Yes I do have a sample for you, this is a continuous problem from my last thread http://www.mathworks.com/matlabcentral/answers/294402-create-plot-from-large-data-in-excel
The code that I tried:
[data,text,raw] = xlsread('run.xlsx');
lgnd = text(2:3,1);
xtlbl = text(1,2:end);
x = 1:size(data,2);
figure(1)
plot(x, data, 's-')
legend(lgnd, 'Location','SE')
set(gca, 'XTick', x, 'XTickLabel',xtlbl, 'YTick',[2 4])
ylim([0 9])
ax = gca;
ax.YGrid = 'on';
ax.GridLineStyle = '-';
only gives plot but no gridline
Adam
Adam 2016년 7월 13일
I doubt it will solve this problem as I'm sure your instructions are being applied to the correct axes anyway, but I would suggest re-arranging your code above as follows (untested, just off the top of my head, but I think it should work).
That is, get the axes handle straight away (I do this on the same line as creating a figure to ensure that focus does not change between creation of the figure and the axes) and store it, then use the explicit axes handle for further instructions instead of keep using gca which is subject to the whims of whatever axes in focus if other figures exist and get clicked on etc, especially if you break in the code with a breakpoint between instructions:
figure(1); ax = gca;
plot(ax, x, data, 's-')
legend(ax, lgnd, 'Location','SE')
set(ax, 'XTick', x, 'XTickLabel',xtlbl, 'YTick',[2 4])
ylim(ax, [0 9])
ax.YGrid = 'on';
ax.GridLineStyle = '-';
chlor thanks
chlor thanks 2016년 7월 13일
편집: chlor thanks 2016년 7월 14일
Now I am sad, this didn't work... but thank you though! I appreciate your inputs as I am still learning everything little by little.
When I tried your code, it looks like the same results:
dpb
dpb 2016년 7월 13일
Attach the figure you get from the above code so we can see what it is that you're actually complaining about.
What happens if you simply do
plot(randn(5,1))
gca.YGrid='on';
? Also attach the results of the above.
chlor thanks
chlor thanks 2016년 7월 14일
편집: chlor thanks 2016년 7월 14일
This is what I got when I simply do
plot(randn(5,1))
gca.YGrid='on';
I wonder y...
chlor thanks
chlor thanks 2016년 7월 14일
I edited the comment now all my plotting results are in the previous comments as well.
Adam
Adam 2016년 7월 14일
plot(randn(5,1))
gca.YGrid='on';
is not valid syntax to do this. You are actually creating a struct called 'gca' in that scenario. gca does not support the new class notation so you would have to use:
plot(randn(5,1))
set( gca, 'YGrid', 'on' );
but that won't change your original code where you do don't do this anyway.
chlor thanks
chlor thanks 2016년 7월 14일
This works! Many thanks Adam!!!
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일

0 개 추천

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일

22 개 추천

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

댓글 수: 3

Ata Chizari
Ata Chizari 2024년 1월 12일
Thanks, I needed it.
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')

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Axis Labels에 대해 자세히 알아보기

태그

질문:

2016년 7월 12일

편집:

2024년 2월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by