Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to draw a vertical line at the peak of a graph

Subject: How to draw a vertical line at the peak of a graph

From: Nehal

Date: 16 Nov, 2010 07:25:05

Message: 1 of 14

I have a graph something like this: http://i54.tinypic.com/oqxbfb.jpg
I have multiple peak values (at y-axis) starting from x=10. I want to draw a vertical line at the first peak value. How can I do that..?

Subject: How to draw a vertical line at the peak of a graph

From: Grzegorz Knor

Date: 16 Nov, 2010 08:16:04

Message: 2 of 14

"Nehal " <arnab620@yahoo.com> wrote in message <ibtbkh$4hc$1@fred.mathworks.com>...
> I have a graph something like this: http://i54.tinypic.com/oqxbfb.jpg
> I have multiple peak values (at y-axis) starting from x=10. I want to draw a vertical line at the first peak value. How can I do that..?

% creating data
x = (0:.1:3)';
y = tanh(x);
y(15) = y(15)+.1;

% find peaks
a=[false; (y(2:end-1)>y(3:end)) & (y(2:end-1)>y(1:end-2)); false];%lokalne minima

% plot
plot(x,y)
hold on
plot(x(a),y(a),'ok')

% find first peak and draw line
ind = find(a==1,1,'first');
yl = get(gca,'Ylim');
line([x(ind) x(ind)],yl,'color','r')

Grzegorz

Subject: How to draw a vertical line at the peak of a graph

From: Nehal

Date: 17 Nov, 2010 16:02:02

Message: 3 of 14

"Grzegorz Knor" <gknor@o2.pl> wrote in message <ibtek3$38b$1@fred.mathworks.com>...
> "Nehal " <arnab620@yahoo.com> wrote in message <ibtbkh$4hc$1@fred.mathworks.com>...
> > I have a graph something like this: http://i54.tinypic.com/oqxbfb.jpg
> > I have multiple peak values (at y-axis) starting from x=10. I want to draw a vertical line at the first peak value. How can I do that..?
>
> % creating data
> x = (0:.1:3)';
> y = tanh(x);
> y(15) = y(15)+.1;
>
> % find peaks
> a=[false; (y(2:end-1)>y(3:end)) & (y(2:end-1)>y(1:end-2)); false];%lokalne minima
>
> % plot
> plot(x,y)
> hold on
> plot(x(a),y(a),'ok')
>
> % find first peak and draw line
> ind = find(a==1,1,'first');
> yl = get(gca,'Ylim');
> line([x(ind) x(ind)],yl,'color','r')
>
> Grzegorz

is there any easier way..?

Subject: How to draw a vertical line at the peak of a graph

From: Sean de

Date: 17 Nov, 2010 16:12:04

Message: 4 of 14

> is there any easier way..?

Yes.

% creating data
x = (0:.1:3)';
y = tanh(x);
y(15) = y(15)+.1;
plot(x,y)
yline_loc = y(diff(y)<0);
hold on
plot([x(1),x(end)],[yline_loc,yline_loc])

Subject: How to draw a vertical line at the peak of a graph

From: Nehal

Date: 17 Nov, 2010 16:20:05

Message: 5 of 14

"Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ic0usk$r0e$1@fred.mathworks.com>...
> > is there any easier way..?
>
> Yes.
> y(15) = y(15)+.1;

why is everyone using this line..? what does it do..?

Subject: How to draw a vertical line at the peak of a graph

From: Sean de

Date: 17 Nov, 2010 16:31:05

Message: 6 of 14

"Nehal " <arnab620@yahoo.com> wrote in message <ic0vbk$rid$1@fred.mathworks.com>...
> "Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ic0usk$r0e$1@fred.mathworks.com>...
> > > is there any easier way..?
> >
> > Yes.
> > y(15) = y(15)+.1;
>
> why is everyone using this line..? what does it do..?

It adds the first peak. Did you even look at either of the above codes?

Subject: How to draw a vertical line at the peak of a graph

From: Sean de

Date: 17 Nov, 2010 16:33:06

Message: 7 of 14

"Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ic0usk$r0e$1@fred.mathworks.com>...
> > is there any easier way..?
>
> Yes.
>
> % creating data
> x = (0:.1:3)';
> y = tanh(x);
> y(15) = y(15)+.1;
> plot(x,y)

%%
> yline_loc = y(diff(y)<0);
> hold on
You'll need to change the last line to this, you want only the first peak.
plot([x(1),x(end)],[yline_loc(1),yline_loc(1)])

Subject: How to draw a vertical line at the peak of a graph

From: Sean de

Date: 17 Nov, 2010 18:41:04

Message: 8 of 14

"Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ic0usk$r0e$1@fred.mathworks.com>...
> > is there any easier way..?
>
> Yes.
>
> % creating data
> x = (0:.1:3)';
> y = tanh(x);
> y(15) = y(15)+.1;
> plot(x,y)
> yline_loc = y(diff(y)<0);
> hold on
> plot([x(1),x(end)],[yline_loc,yline_loc])

I misread that earlier, you wanted a vertical, not horizontal line:
%
% creating data
x = (0:.1:3)';
y = tanh(x);
y(15) = y(15)+.1;

xloc = x(diff(y)<0); %maxima

plot(x,y,'b-');
hold on
plot([xloc(1) xloc(1)],[min(y) max(y)],'r-')

Subject: How to draw a vertical line at the peak of a graph

From: Nehal

Date: 18 Nov, 2010 04:03:07

Message: 9 of 14

"Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ic1009$9qu$1@fred.mathworks.com>...
> "Nehal " <arnab620@yahoo.com> wrote in message <ic0vbk$rid$1@fred.mathworks.com>...
> > "Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ic0usk$r0e$1@fred.mathworks.com>...
> It adds the first peak. Did you even look at either of the above codes?

yes I have.. but it's hard to find "x" in my case.. because I have a whole 300-350 line code and it saves the results sequentially. I want to plot the results for different values of x. This is why I am facing some problem. But your reply definitely helped me.

Subject: How to draw a vertical line at the peak of a graph

From: Nehal

Date: 18 Nov, 2010 04:37:05

Message: 10 of 14

"Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ic17jv$4l0$1@fred.mathworks.com>...
> "Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ic0usk$r0e$1@fred.mathworks.com>...
> I misread that earlier, you wanted a vertical, not horizontal line:
> %
> % creating data
> x = (0:.1:3)';
> y = tanh(x);
> y(15) = y(15)+.1;
>
> xloc = x(diff(y)<0); %maxima
>
> plot(x,y,'b-');
> hold on
> plot([xloc(1) xloc(1)],[min(y) max(y)],'r-')

the code you have provided, it shows a vertical line at the first peak at x=5. But not the first "highest" peak. Something like this: http://img140.imageshack.us/img140/2461/plot97.jpg
I need the line at the first highest peak. In my case, that first highest peak is at x=10.

Any help..?

Subject: How to draw a vertical line at the peak of a graph

From: Grzegorz Knor

Date: 18 Nov, 2010 07:24:03

Message: 11 of 14

"Nehal " <arnab620@yahoo.com> wrote in message <ic2ahg$phu$1@fred.mathworks.com>...
> "Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ic17jv$4l0$1@fred.mathworks.com>...
> > "Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ic0usk$r0e$1@fred.mathworks.com>...
> > I misread that earlier, you wanted a vertical, not horizontal line:
> > %
> > % creating data
> > x = (0:.1:3)';
> > y = tanh(x);
> > y(15) = y(15)+.1;
> >
> > xloc = x(diff(y)<0); %maxima
> >
> > plot(x,y,'b-');
> > hold on
> > plot([xloc(1) xloc(1)],[min(y) max(y)],'r-')
>
> the code you have provided, it shows a vertical line at the first peak at x=5. But not the first "highest" peak. Something like this: http://img140.imageshack.us/img140/2461/plot97.jpg
> I need the line at the first highest peak. In my case, that first highest peak is at x=10.
>
> Any help..?

At the begining you wrote that you want to draw a vertical line at the first peak value. What does mean 'first "highest" peak'? Only one value can be a highest.

This is solution of your problem:
plot([max(xloc) max(xloc)],[min(y) max(y)],'r-')
?

Grzegorz

Subject: How to draw a vertical line at the peak of a graph

From: Greg Heath

Date: 18 Nov, 2010 10:11:02

Message: 12 of 14

On Nov 17, 11:37 pm, "Nehal " <arnab...@yahoo.com> wrote:
> "Sean de " <sean.dewol...@nospamplease.umit.maine.edu> wrote in message <ic17jv$4l...@fred.mathworks.com>...
>
> > "Sean de " <sean.dewol...@nospamplease.umit.maine.edu> wrote in message <ic0usk$r0...@fred.mathworks.com>...
> > I misread that earlier, you wanted a vertical, not horizontal line:
> > %
> > % creating data
> > x = (0:.1:3)';
> > y = tanh(x);
> > y(15) = y(15)+.1;
>
> > xloc = x(diff(y)<0); %maxima
>
> > plot(x,y,'b-');
> > hold on
> > plot([xloc(1) xloc(1)],[min(y) max(y)],'r-')
>
> the code you have provided, it shows a vertical line at the first peak at x=5. But not the first "highest" peak. Something like this:http://img140.imageshack.us/img140/2461/plot97.jpg
> I need the line at the first highest peak. In my case, that first highest peak is at x=10.
>
> Any help..?

close all, clear all, clc

T = 2*pi, N = 32, dt = T/N
t = dt*(0:N-1);
y = repmat(sin(t)+ cos(3*t),1,2);
t = dt*(0:2*N-1);

[ymax indmax] = max(y)
tpeak = t(indmax)
figure
hold on
plot(t,zeros(1,2*N),'k')
plot(t,y)
plot([tpeak,tpeak],[0,ymax],'r-')

Hope this helps.

Greg

Subject: How to draw a vertical line at the peak of a graph

From: Nehal

Date: 18 Nov, 2010 15:46:06

Message: 13 of 14

"Grzegorz Knor" <gknor@o2.pl> wrote in message <ic2kaj$ml9$1@fred.mathworks.com>...
> "Nehal " <arnab620@yahoo.com> wrote in message <ic2ahg$phu$1@fred.mathworks.com>...
> > "Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ic17jv$4l0$1@fred.mathworks.com>...
> > > "Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ic0usk$r0e$1@fred.mathworks.com>...
> At the begining you wrote that you want to draw a vertical line at the first peak value. What does mean 'first "highest" peak'? Only one value can be a highest.
>
> This is solution of your problem:
> plot([max(xloc) max(xloc)],[min(y) max(y)],'r-')
> ?

actually in my case, I have 30 values. The value is 100 started from the 10th value till the 30th values. I can see it by observing the values. But I wanted to see it on graph too. That's why I have multiple highest values (which is 100 in my case). And the first 100 is on the 10th position.

But the solution you have given, it draws a vertical line on x=15. At x=15, I have a value of 100. But that was not the first 100. At x=10, I have my first 100. So what's now..?

Subject: How to draw a vertical line at the peak of a graph

From: Nehal

Date: 18 Nov, 2010 15:59:05

Message: 14 of 14

Greg Heath <heath@alumni.brown.edu> wrote in message <b798e2cd-b666-4416-9def-f9f8c7cfb80c@30g2000yql.googlegroups.com>...
> On Nov 17, 11:37 pm, "Nehal " <arnab...@yahoo.com> wrote:
> > "Sean de " <sean.dewol...@nospamplease.umit.maine.edu> wrote in message <ic17jv$4l...@fred.mathworks.com>...
> >
> > > "Sean de " <sean.dewol...@nospamplease.umit.maine.edu> wrote in message <ic0usk$r0...@fred.mathworks.com>...
> T = 2*pi, N = 32, dt = T/N
> t = dt*(0:N-1);
> y = repmat(sin(t)+ cos(3*t),1,2);
> t = dt*(0:2*N-1);
>
> [ymax indmax] = max(y)
> tpeak = t(indmax)
> figure
> hold on
> plot(t,zeros(1,2*N),'k')
> plot(t,y)
> plot([tpeak,tpeak],[0,ymax],'r-')
>
> Hope this helps.
>
> Greg

yah.. it helped.. thank you.. :)

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us