How do I plot wind direction timeseries with arrows

조회 수: 72 (최근 30일)
giacomo labbri
giacomo labbri 2020년 6월 12일
편집: David 2022년 1월 21일
Hi,
I would like to plot a timeseries of wind direction. I would like represent the direction at each point in time with an arrow. The direction of the wind is expressed in degrees. I have saw that quivers might do what I want with some tricks but I was wondering if there was a more specific function.
Thanks in advance,
Giacomo
  댓글 수: 3
Adam Danz
Adam Danz 2020년 6월 12일
편집: Adam Danz 2020년 6월 14일
As my answer indicates, the quiver function is what you should use given the description of the problem. However, the quiver function does not accept datetime values. It only accepts numeric values or values that can be converted to double. That's for a good reason. Imagine a vector whose base is at x="January 1, 2000" and y = 10 and has (u,v) components of (2,1) (in other words, it extends 2 units in the x direction and 1 unit in the y direction). What does that mean with datetime values?
What is "2" in datetime? 2 days? 2 hours? Matlab wouldn't be able to figure that out. But you could figure that out since you know the context of your data.
So, you'll need to convert the datetime values in order to use quiver. Then you can set the x tick labels aftewards to datetime values using datetick.
giacomo labbri
giacomo labbri 2020년 6월 13일
thanks that was really helpful! :)

댓글을 달려면 로그인하십시오.

채택된 답변

Adam Danz
Adam Danz 2020년 6월 12일
편집: Adam Danz 2022년 1월 21일
The quiver() function is what you want to use. It's desiged exactly for this purpose.
Since your vectors are in degrees and the quiver function requires the x,y component vectors, use pol2cart() to convert your polar vectors to cartesian vectors. You'll also need to convert deg-->rad for the pol2cart inputs.
% WindDirection is the direction of wind (deg)
% WindSpeed is the magnitude of the wind vector
[xWind, yWind] = pol2cart((pi/180).*WindDirection, WindSpeed);
quiver(x, y, xWind, yWind)
  댓글 수: 3
David
David 2022년 1월 20일
편집: David 2022년 1월 21일
One issue with this is that the the angles won't necessarily line up. If you have a lot of x points, it will compress the xWind component and throw off the angles.
There was an extra parenthesis. Try this:
% WindDirection is the direction of wind (deg)
% WindSpeed is the magnitude of the wind vector
[xWind, yWind] = pol2cart((pi/180).*WindDirection, WindSpeed);
quiver(x, y, xWind, yWind)
Adam Danz
Adam Danz 2022년 1월 21일
Thanks for pointing out the typo. Fixed.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

David
David 2022년 1월 20일
편집: David 2022년 1월 21일
A problem with quiver for this is that the horizontal axis is shared by both time and the x component of windspeed, so you have to make choices about how you scale things if you want northeast winds to go on the 45° angle and the y axis to measure speed
If you leave the aspect ratio free, the date axis is correct, but the 45° winds won't be proper.
x = datenum(t); y=x*0;
quiver(x,x*0, xWind, yWind,0,'.');
datetick;
If you make the aspect ratio equal (`axes equal`) then the 45° will be proper, but you may need to scale the time data to see your data, and then that screws up the automatic date tick labelling. Then getting the time tickmarks right is tricky. You can capture the automatically generated tickmarks, find the corresponding times, and then replace the labels with the corresponding times. See https://www.mathworks.com/matlabcentral/answers/391904-datetick-at-feather-plot#answer_312939
If you don't need the dateticks, this works well for maintaing angles through resizing and zooming:
sfact = 1000; % or whatever looks nice
x = datenum(t); y=x*0;
quiver(x*sfact,x*0, xWind, yWind,0,'.');
axis equal;
set(gca,'XTickLabel',[],'YTickLabel',[])
% ... tickmark replacement magic
Or if you keep the time axis, and the yWind, you could scale the xWind by a factor to match the aspect ratio between time and yWind, but then you can't resize/reshape your plot without decalibrating the angles.
I like this one best if you want to label the time axis:
x = datenum(t);
y= x*0;
quiver(x, y, xWind, yWind,0,'.')
x = datenum(t); y=x*0;
sfact = 1;
rg=1000:3200;
quiver(x(rg),x(rg)*0, xWind(rg)*sfact, yWind(rg),0,'.');
ylim([-10,10]);
xlim([min(x(rg)),max(x(rg))])
% asjust plot to desired size/shape/aspect
datetick;
% capture the aspect ratio between time and yWind
dd=daspect;ax=gca; sfact = 1/(dd(2)/dd(1)/ax.PlotBoxAspectRatio(2));
[dd(2)/dd(1) ax.PlotBoxAspectRatio dd(2)/dd(1)/ax.PlotBoxAspectRatio(2) sfact]
quiver(x(rg),x(rg)*0, xWind(rg)*sfact, yWind(rg),0,'.'); %replot scaling xWind to match time
ylim([-10,10]);xlim([min(x(rg)),max(x(rg))])
datetick; % add dateticks

카테고리

Help CenterFile Exchange에서 Vector Fields에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by