Plotting wind magnitude time series and direction as indicator arrows

조회 수: 11 (최근 30일)
Nayef Zrigat
Nayef Zrigat 2022년 5월 20일
답변: VINAYAK LUHA 2023년 9월 22일
Hi all,
I am trying to plot wind time series for 1 point, where I have magnitude and direction. I would like to have a regular 2D plot of the magnitude but then I want to add an arrow indicating the direction at each plotted point in time, as shown in the attached figure.
would appreciate any help executing this or other suggestions to plot magnitude and direction time series (other than the straightforward ways obviously (e.g.,quiver..etc)
Cheers,
NZ

답변 (1개)

VINAYAK LUHA
VINAYAK LUHA 2023년 9월 22일
Hi Nayef,
It is my understanding that you have a time series dataset where each datapoint corresponds to some wind magnitude, additionally you have an array representing the wind direction at these datapoints. You want to know an alternate way than using the traditional “quiver” function to represent wind direction using arrows at these plotted data points.
Here’s a possible workaround for your reference-
Hi Nayef,
It is my understanding that you have a time series dataset where each datapoint corresponds to some wind magnitude, additionally you have an array representing the wind direction at these datapoints. You want to know an alternate way than using the traditional “quiver” function to represent wind direction using arrows at these plotted data points.
Here’s a possible workaround for your reference-
% Generating random data
time = 1:10;
magnitude = [3 6 4 8 5 7 6 9 4 7];
direction = [30 45 60 75 90 105 120 135 150 165]; %in degrees
% Plotting magnitude
plot(time, magnitude, 'b', 'LineWidth', 2);
hold on;
% Add arrows indicating the direction
arrowLength = max(magnitude) * 0.05;
arrowX = time;
arrowY = magnitude;
arrowAngle = deg2rad(direction);
arrowXEnd = arrowX + arrowLength * cos(arrowAngle);
arrowYEnd = arrowY + arrowLength * sin(arrowAngle);
for i = 1:numel(time)
drawArrow([arrowX(i), arrowY(i)], [arrowXEnd(i), arrowYEnd(i)], 'r', 0.08, 25);
end
% Custom function to draw an arrow
function drawArrow(startPoint, endPoint, color, lineWidth, tipAngle)
% Calculate arrowhead points
arrowLength = norm(endPoint - startPoint);
arrowHeadLength = arrowLength * tan(deg2rad(tipAngle));
arrowHeadWidth = arrowHeadLength / 2;
arrowHeadPoints = [0, arrowHeadWidth; arrowHeadLength, 0; 0, -arrowHeadWidth];
% Rotate and translate arrowhead points
theta = atan2(endPoint(2) - startPoint(2), endPoint(1) - startPoint(1));
R = [cos(theta), -sin(theta); sin(theta), cos(theta)];
arrowHeadPoints = (R * arrowHeadPoints')';
arrowHeadPoints = arrowHeadPoints + endPoint;
% Draw arrow
line([startPoint(1), endPoint(1)], [startPoint(2), endPoint(2)], 'Color', color, 'LineWidth', lineWidth);
patch(arrowHeadPoints(:, 1), arrowHeadPoints(:, 2), color, 'EdgeColor', color, 'LineWidth', lineWidth);
end
Regards
Vinayak Luha

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by