Combined plot

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Combined plot

Blaz
Hello
I have two issues to solve in Octave.

First issue

 I have a vector [x]. Is there a simple way to create two new vectors (e.g.
xodd and xeven)? xodd is (obviously) composed from odd index-es of x and
xeven from even ones. I have written the code below, but it fills the blanks
with zeros, which is not ok. I would like to have only the extracted values
in new vectors.

t = [0:0.001:0.03];
x = sin(2*pi*50*t);
for i=1:length(t)
  if rem(i,2) == 0
    xeven(i)=x(i);
  end  
  if rem(i,2) == 1
    xodd(i)=x(i);
  end
end


Second issue

When I do have x, xodd and xeven vectors properly calculated I wish to plot
them as following

1. x as a simple plot (x) on first diagram (e.g . subplot (311))
2. x as a simple plot (x) and xeven as arrows between x-axis and plotted x
curve (subplot 312)
3. x as simple plot(x) and xodd as arrows between x-axis and plotted x curve
(subplot 313)

I tried with bar (xodd) and bar(xeven), but I would like to plot the arrows
instead of bars.

Any help is much appreciated

Thanks



--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: Combined plot

mmuetzel
Am 27. Januar 2021 um 08:59 Uhr schrieb "Blaz":

> Hello
> I have two issues to solve in Octave.
>
> First issue
>
>  I have a vector [x]. Is there a simple way to create two new vectors (e.g.
> xodd and xeven)? xodd is (obviously) composed from odd index-es of x and
> xeven from even ones. I have written the code below, but it fills the blanks
> with zeros, which is not ok. I would like to have only the extracted values
> in new vectors.
>
> t = [0:0.001:0.03];
> x = sin(2*pi*50*t);
> for i=1:length(t)
>   if rem(i,2) == 0
>     xeven(i)=x(i);
>   end
>   if rem(i,2) == 1
>     xodd(i)=x(i);
>   end
> end
>

You could use this:
xeven = x(2:2:end);
xodd = x(1:2:end);


> Second issue
>
> When I do have x, xodd and xeven vectors properly calculated I wish to plot
> them as following
>
> 1. x as a simple plot (x) on first diagram (e.g . subplot (311))
> 2. x as a simple plot (x) and xeven as arrows between x-axis and plotted x
> curve (subplot 312)
> 3. x as simple plot(x) and xodd as arrows between x-axis and plotted x curve
> (subplot 313)
>
> I tried with bar (xodd) and bar(xeven), but I would like to plot the arrows
> instead of bars.
>

`quiver` draws arrows. Maybe you could use that function for your purposes.

HTH,
Markus



Reply | Threaded
Open this post in threaded view
|

Re: Combined plot

Blaz
Hello Markus

Yes, I have tried /quiver/, but the result is not what I wanted. Probably I
am using it wrong. I have managed to do something similar with /bar/, but I
would like to replace bars with arrows.

odev.png <https://octave.1599824.n4.nabble.com/file/t373917/odev.png>  



--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: Combined plot

mmuetzel
Am 27. Januar 2021 um 17:38 Uhr schrieb "Blaz":
> Hello Markus
>
> Yes, I have tried /quiver/, but the result is not what I wanted. Probably I
> am using it wrong. I have managed to do something similar with /bar/, but I
> would like to replace bars with arrows.
>
> odev.png <https://octave.1599824.n4.nabble.com/file/t373917/odev.png>

It's not easy to get esthetically pleasing results using `quiver` with your data. IIUC that is because the scale in x and y direction is very different.
This might come close to what you might want:
t = [0:0.001:0.03];
x = sin(2*pi*50*t);
clf;
plot(t, x);
hold on
teven = t(2:2:end);
xeven = x(2:2:end);
quiver(teven, zeros(size(teven)), zeros(size(xeven)), xeven, 0, 'maxheadsize', 0.002);

The arrow heads look "nicer" if the scale in x and y is approximately equal:
t = [0:0.001:0.03]*100;  % use different scale in x direction
x = sin(2*pi*50*t);
clf;
plot(t, x);
hold on
teven = t(2:2:end);
xeven = x(2:2:end);
quiver(teven, zeros(size(teven)), zeros(size(xeven)), xeven, 0, 'maxheadsize', 0.05);

Maybe you could work with that and manually adjust the "xticklabel" property of the axes.


Or manually place `annotation` objects at the positions you need. See e.g. `demo annotation 6`.

HTH,
Markus