How to create a stickplot?

Hello,
I am trying to create a stickplot in MATLAB (velocity vector vs time), for which I'm using the stickplot.m function (attached). I have also attached a demo so you can see the result of the function. I need to change the size of the vectors, can you help me? It would be much appreciated, I'm struggling with this for over a week now.
Thank you in advance

 Accepted Answer

Mathieu NOE
Mathieu NOE on 15 Dec 2020
hello
I interpreted your request as you want to change the LineWidth of the vector so it can look thicker
I added this parameter in the function - very easy
see the results (attached)

25 Comments

Emi
Emi on 15 Dec 2020
Edited: Emi on 15 Dec 2020
Thank you for your answer, but I want to change the length of the vectors, not their thickness. For example: https://www.researchgate.net/profile/Praveenkhanna_Udayakumar/post/How-do-I-make-stick-plots-for-wind-vectors/attachment/59d6226d6cda7b8083a1c153/AS%3A272071309656066%401441878263942/image/image_preview.jpg : here, the standard length/unit of the vectors is 5 m/s. How can I do something similar with my data?
sorry, I am not sure to understand
I've looked at the picture you mentionned in the link
the length is related to the data amplitude, if you input 1 it will display 1
or do you mean, you would like to have like in the picture a "scale" meter displayed above the data ?
it seems to me that is already what the stickplot is doing , but I'll look again
hi again
as far as I have understood how stickplot works it will display that "unit" meter which length is correct and consistent with data amplitude
maybe you are looking for other options , like those discussed here :
This is great, because I did need to add a scale, but I still have to change the actual length of the vectors, because when I create the plot, the vectors overlap one another. So, it looks quite complicated due to their lengths, as you can see in the attached image.
Also, I don't understand the use of 'unit' in the function stickplot.m.
huh , how did you generate that plot ??
I modified a bit your code to change the vector amplitude, but even with large values , the plot remains clean and readable
% Fake a data set
n = 100;
date=1:n;
amplitude = 50;
vx=[ filter(hamming(20),1,amplitude*2*(rand(1,n)-.5));
filter(hamming(20),1,amplitude*2*(rand(1,n)-.5)) ]';
vy=[ filter(hamming(20),1,amplitude*2*(rand(1,n)-.5));
filter(hamming(20),1,amplitude*2*(rand(1,n)-.5)) ]';
LineWidth = 2.5;
stickplot(date,vx,vy,[0 n],'m/s',['series 1';'series 2'],LineWidth);
title('Everything done by STICKPLOT!');
and 'unit' let you scpecify what unit (text) will be displayed
in my demo I changed to m/s
beside this , do you know feather ?
theta = (-90:10:90)*pi/180;
r = 2*ones(size(theta));
[u,v] = pol2cart(theta,r);
feather(u,v), axis equal
Ok, I understood the unit's purpose.
How can I make the vectors look smaller? I think this is why the plot looks so complicated. (If you want I can send you a part of the data to test it out yourself).
My data have values roughly between -0.7m/s to 0.7m/s, but the unit in the stickplot function is 1. Could this be the problem?
Also, as you can see in the link I sent you - https://www.researchgate.net/profile/Praveenkhanna_Udayakumar/post/How-do-I-make-stick-plots-for-wind-vectors/attachment/59d6226d6cda7b8083a1c153/AS%3A272071309656066%401441878263942/image/image_preview.jpg - there is horizontal line seperating the positive from the negative values. How can I do this? This, though, is a minor problem right now.
yes please share your data also
the horizontal line can be added on top , this is no bid deal
have you looked at feather ?
I have looked at it in the past, I will check it out again if stickplot doesn't work for me.
Ok, I have attached some of my data. They are measurements, so a lot of them are NaN and some of them, as you will see, are obviously wrong, so you will have to exclude them yourself (I usually use the "brush" tool). Thank you so much for your help.
ok
so going back to your comment : the vectors overlap one another.
yes they do , but that has nothing to do with stickplot (I compared it to quiver) , it is because when you compute the angle between u and v, this angle changes very abruptly , and that makes the vectors overlap
changing scale factors will not do anything here, it's like zooming back or forth into the image , but this will not change the angle randomness
either the data have to be "truly" displayed this way, or you want somehow to smooth out this angle behaviour
that's what I implemented , but use it at your own risks !! I am not sure this is the way to go
also I modified the stickplot function so you have a reference line
I believe there is somewhere a problem with the stickplot function
I made the scale factor tunable from the main script
if yoy look at stickdemo3.m and modify this value , the length of the vector should be changed but not the angle; as this is not true , I suspect a problem there
if you increase the scale factor , the vectors become more vertical which shouldn't be the case
Emi
Emi on 15 Dec 2020
Edited: Emi on 15 Dec 2020
When I used the modificated stickplot function, the length of the vectors didn't become smaller... They either become bigger (when decreasing the scale factor) or their angles change (when increasing the scale factor the angles become smaller), as you said. I am confused.
As you said, there might be a problem with the stickplot, but I still need to make this plot. Given the data I gave to you in the last comment, can we plot sth similar with the feather function?
P.S. I can't "mess" with the data because they are actual measurements.
ok
my question to you is how important is it to respect the "true" angle between the u and v components ?
if you want to be 100% exact , the figure axes are to be equal (so there should be axes equal somewhere in the code)
now the problem if you put axes equal in the code, and if you have a very long time axes (says between 0 and 1000) and you data amplitude is only 1 , you would barely see anything in the plot; so , if you have an "auto scaling" axe display by default, this will zoom in the y axe to make your data visible, but by doing this only in one direction,it also distort the angle of vector (expect the purely horizontal and vertical ones); but a 45° oriented vector will not look with an angle of 45° if the autoscaling property has magnified the y axe by a factor 1000, the perceived angle will be close to 90° so this is not a "true" rendering of your data
The angles need to stay true.
Is there another way to create this plot? Beside using the stickplot function?
you can keep the angle true , but the definition of "true" depends if we force the u and v directions to share the same unit length or not
in the first case that means we need to implement
axis equal
and then you get the arrows crossing each others whatever scale factor you choose
IMHO, the only way to make the vectors less "random" looking is to have two different axe unit lengths (scales), but that is a way to make the display nicer without doing any mathematical mess
but you have to keep in mind u and v scales are differents and make sure this is clearly displayed in the legend
do we want to go that route ?
attached the last version of stick plot with axis equal
with quiver , I also tried to play with a scale factor, but the results are similar : you can decide to reduce the scale factor to not have the arrows crossing but the length is so small you don't see anything
Emi
Emi on 16 Dec 2020
Edited: Emi on 16 Dec 2020
First of all, I really appreciate your help and the effort you put in the problem, so I want to thank you for that - noone has really helped me with this and I have been struggling for so long. I am sure there is a way to create that plot.
To answer your question, I don't want to have two different scales for u and v. As you saw in the image I sent to you, there is only one scale. The vectors may overlap a little, but the movement of the vectors is clear.
How did you create the plot using quiver? Could you please share the code?
I also have data for the speed and the direction - using these I caculated the components u and v. So if there is a function using as input the direction and the speed, we could try that as well.
you're welcome ! it's my pleasure trying to fix those little problems ;
the code for quiver is at the end of stickdemo2.m :
figure(2),
scale = 1;
quiver(t,0,u,v,scale)
axis equal
I am ok to look at the speed / direction data if you can share that too
regards
I have attached the speed (uv) and the direction (dir) of my data. I used these two variables to calculate the u, v constituents.
The quiver plot looks actually quite nice! Can I change the size of the arrows' heads? Can I delete their heads completely?
P.S. I think there actually is a problem with the stickplot function.
so this is a code that will display the three options
I would say quiver is OK, and you can remove the arrow head by specifying the linespec as '.'
seems it is not doable with feather, by the way, for me does not make much difference with quiver
I am still believing stickplot does not 100% respect vectors direction , even with axis equal; still need to figure that out
% STICKDEMO This demo shows what you can do easily with STICKPLOT
% RP (WHOI) 15/Jan/91
clc
clear all
close all
% load u.mat
% load v.mat
% load t.mat
load uv.mat
load dir.mat
% my preference : replace NaN with zero
uv(isnan(uv)) = 0;
dir(isnan(dir)) = 0;
u = uv.*cosd(dir);
v = uv.*sind(dir);
t = 1:length(uv);
% % display only first n samples (rest is zero)
% n = 500;
% t = t(1:n);
% u = u(1:n);
% v = v(1:n);
LineWidth1 = 2.5;
LineWidth2 = 1.5;
scale_factor = 8*2; % original value is 8, adapt to your onw needs (increase it if vectors overlap)
figure(1),stickplot(t,u,v,[0 length(t)],'m/s',['series 1'],LineWidth1,LineWidth2,scale_factor);grid
title('No angle smoothing');
figure(2),
scale = 3;
quiver(t,0,u,v,scale,'.') ,axis equal
title('quiver - no arrow ');
figure(3),
scale = 200;
feather(scale*u,scale*v,'-'), axis equal
title('feather - arrow only possible');
% QUIVER(U,V,S) or QUIVER(X,Y,U,V,S) automatically scales the
% arrows to fit within the grid and then stretches them by S. Use
% S=0 to plot the arrows without the automatic scaling.
should have mentionned that I put the figure windows as big as possible (full screen size) to get best results
Thank you very much for your help!
you're welcome !
Dear Mathieu
Stick plot is Very Beautiful and meaning full.
I tried your stick plot function for my data set.
It makes error, (Uf or variable 'myslidingavg')
Highly appreciate your help for correct this.
Kind Regard
INDIKA
% STICKDEMO This demo shows what you can do easily with STICKPLOT
% RP (WHOI) 15/Jan/91
clc
clear all;
load power_2022new.txt;
u=power_2022new(:,4);
v=power_2022new(:,5);
t=power_2022new(:,1);
% load u.mat
% load v.mat
% load t.mat
% my preference : replace NaN with zero
u(isnan(u)) = 0;
v(isnan(v)) = 0;
% display only first n samples (rest is zero)
%n = 500;
n = 366; % year data
t = t(1:n);
u = u(1:n);
v = v(1:n);
% smooting the angle
ind = find(abs(u)>eps & abs(v)>eps);
theta = zeros(size(t));
theta(ind) = atan(v(ind)./u(ind));
N_smooth = 3; % nb op points used in the averaging (the higher the smoother)
theta_smoothed = myslidingavg(theta, N_smooth);
mod = sqrt(u.^2+v.^2);
u_smoothed = mod.*cos(theta_smoothed);
v_smoothed = mod.*sin(theta_smoothed);
LineWidth1 = 2.5;
LineWidth2 = 1.5;
scale_factor = 8; % original value is 8, adapt to your onw needs (increase it if vectors overlap)
figure(1),stickplot(t,u,v,[0 length(t)],'m/s',['series 1'],LineWidth1,LineWidth2,scale_factor);grid
title('No angle smoothing');
% figure(2),plot(t,theta,'b',t,theta_smoothed,'r')
% figure(3),stickplot(t,u_smoothed,v_smoothed,[0 length(t)],'m/s',['series 1'],LineWidth1,LineWidth2,scale_factor);grid
% title('With angle smoothing');
% % figure,plot(t,u,'-+b',t,v,'-+r');grid
% %
% % figure, feather(u,v)
figure(2),
scale = 1;
% quiver(u,v)
quiver(t,0,u,v,scale)
axis equal
% QUIVER(U,V,S) or QUIVER(X,Y,U,V,S) automatically scales the
% arrows to fit within the grid and then stretches them by S. Use
% S=0 to plot the arrows without the automatic scaling.
1 58.5 4.39
2 62.81 5.99
3 69.56 5.83
4 67 4.88
5 110.44 2.09
6 78.25 3.9
7 69.69 4.26
8 40.31 3.02
9 58.81 3.69
10 118.62 2.11
11 125.12 2.11
12 163.56 2.31
13 197.88 2.56
14 126.88 1.77
15 75.06 1.72
16 55.69 4.48
17 62.5 5.11
18 61.44 4.14
19 44.81 4.49
20 130.12 2.73
21 255.25 1.59
22 216 1.54
23 141.62 1.45
24 120.94 1.8
25 93.25 2.52
26 60.5 2.71
27 49.88 3.48
28 76.25 3.53
29 108.25 3.03
30 90.44 3.49
attached is the function myslindingavg
nota that now matlab has also plenty of option to smooth data like smoothdata

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Asked:

Emi
on 6 Dec 2020

Commented:

on 2 Oct 2023

Community Treasure Hunt

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

Start Hunting!