COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### How to draw a vertical line at the peak of a graph

• Email
• Follow

```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..?
```
 0

See related articles to this posting

```"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
```
 0

```"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..?
```
 0

```> 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])
```
 0

```"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..?
```
 0

```"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?
```
 0

```"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)])
```
 0

```"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-')
```
 0

```"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.
```
 0

```"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..?
```
 0

```"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
```
 0

```On Nov 17, 11:37=A0pm, "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 =3D (0:.1:3)';
> > y =3D tanh(x);
> > y(15) =3D y(15)+.1;
>
> > xloc =3D 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=3D5. 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=3D10.
>
> Any help..?

close all, clear all, clc

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

[ymax indmax] =3D max(y)
tpeak =3D 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
```
 0

```"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..?
```
 0

```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.. :)
```
 0

13 Replies
518 Views

Similar Articles

12/1/2013 6:13:21 PM
page loaded in 1616689 ms -1