Plot - Grid problem : I need grid for each increment in x

clear all clc
y=[ 8 0 5 5 4 0 8 1 7 3 5 1 4 8 2 7 2 0 ]; x=[ 1 0 4 0 5 1 5 1 8 3 4 6 4 7 ];
Tx=length(x); Ty=length(y);
for i=1:Tx j(i)=floor((Ty/Tx)*i); end
plot(1:length(j),j) grid on
My doubt is, How can I plot grids for each point of x axis. suppose, for the above program plot showing grid for every 2 increment of x axis. I need for grid for each single step increment so that I can analyz my problem easily.
Please give your answer for my problem.

 Respuesta aceptada

Matt Fig
Matt Fig el 16 de Mzo. de 2011
% First your plot:
y=[8 0 5 5 4 0 8 1 7 3 5 1 4 8 2 7 2 0 ];
x=[ 1 0 4 0 5 1 5 1 8 3 4 6 4 7];
Tx=length(x);
Ty=length(y);
for i=1:Tx
j(i)=floor((Ty/Tx)*i);
end
plot(1:length(j),j)
grid off
% Now make your grid...
YL = get(gca,'ylim');
for ii = 1:length(j)
line([j(ii) j(ii)],YL,'linestyle','--','color','k');
end
.
.
EDIT
Or without the FOR loop:
% Now make your grid...
YL = get(gca,'ylim');
line([j;j],repmat(YL.',1,length(j)),'linestyle','--','color','k')
.
.
EDIT2
I see that you wanted the gridpoints at the x locations, not the j locations. Simply substitute an x where I have put a j...

7 comentarios

Raviteja
Raviteja el 16 de Mzo. de 2011
Thanks!
But I lost Y axis alignments! Please clarify this.
I need y axis grids also.
that is 1 to 1 map grid I needed..
Matt Fig
Matt Fig el 16 de Mzo. de 2011
I don't quite understand. Do you mean you want to have grids where the y's are too? If so, simply add:
XL = get(gca,'xlim');
line(repmat(XL.',1,length(y)),[y;y],'linestyle','--','color','k')
Raviteja
Raviteja el 16 de Mzo. de 2011
Try this code:
clear all
clc
clf
y=[8 0 5 5 4 0 8 1 7 3 5 1 4 8 2 7 2 0 ];
x=[ 1 0 4 0 5 1 5 1 8 3 4 6 4 7];
Tx=length(x);
Ty=length(y);
for i=1:Tx
j(i)=floor((Ty/Tx)*i);
end
plot(1:length(j),j,'r')
hold on
m=Ty/Tx;
y=m*(1:Tx);
plot(y)
YL = get(gca,'ylim');
line([j;j],repmat(YL.',1,length(j)),'linestyle','--','color','k')
XL = get(gca,'xlim');
line(repmat(XL.',1,length(y)),[y;y],'linestyle','--','color','k')
xlim([1 Tx])
here horizontal lines missed its exact positions.
Matt Fig
Matt Fig el 16 de Mzo. de 2011
It looks to me like the horizontal lines got the exact position. Did you take into account that you re-defined your y variable?
Raviteja
Raviteja el 16 de Mzo. de 2011
sorry! I used y variable and modified. It is my mistake.
Next problem is, some grids are missing! try this code!
clear all
clc
clf
y=[8 0 5 5 4 0 8 1 7 3 5 1 4 8 2 7 2 0 ];
x=[ 1 0 4 0 5 1 5 1 8 3 4 6 4 7];
Tx=length(x);
Ty=length(y);
for i=1:Tx
j(i)=floor((Ty/Tx)*i);
end
plot(1:length(j),j,'r')
hold on
m=Ty/Tx;
p=m*(1:Tx);
plot(p)
YL = get(gca,'ylim');
line([j;j],repmat(YL.',1,length(j)),'linestyle','--','color','k')
XL = get(gca,'xlim');
line(repmat(XL.',1,length(y)),[y;y],'linestyle','--','color','k')
xlim([1 Tx])
Matt Fig
Matt Fig el 16 de Mzo. de 2011
And which grids are missing? Did you see my not about changing the j to x if you want vertical grids at the x location?
Matt Fig
Matt Fig el 16 de Mzo. de 2011
Also, (see previous comment) did you notice that you are setting the xlim to be less than the smallest vertical grids?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

el 16 de Mzo. de 2011

Community Treasure Hunt

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

Start Hunting!

Translated by