MATLAB Answers

plot x, y and direction

72 views (last 30 days)
zheng
zheng on 21 Jan 2011
Edited: Walter Roberson on 7 Nov 2018
Hi all,
I have values for x, y, and Azimuth, where Azimuth is in range of 0 to 360 degree measured from the North in clockwise.
how can I plot them in a 2D graph which shows the point with direction arrow?
x
607261.185639785
607280.329476425
607367.827131141
y
4821687.60219055
4821678.02095485
4821646.17145724
direction
108
108
115.142857142857

Answers (7)

Walter Roberson
Walter Roberson on 21 Jan 2011
You probably want to use quiver() to plot your vector fields.

zheng
zheng on 21 Jan 2011
Here is sample I found to deal with my problem
but only question is how to set the angle are measured from North in clockwise (Azimuth)
clear; clc; close all
% Arrows in the following
% red = 0 to 90 degrees (counterclockwise)
% green = 91 to 180 degrees
% blue = 181 to 270 degrees
% cyan = 271 to 360 degrees
% Data is organize as (x, y, theta in degrees)
data = [607261.185639785 4821687.60219055 108
607280.329476425 4821678.02095485 108
607367.827131141 4821646.17145724 115.142857142857
607423.238973159 4821613.59562066 120
];
% Plot to figure 1
figure(1);
% Identify data from 1st quadrant and store in q1, identify data from 2nd quadrant
% goes to q2, and so on. A value of 1 indicates it is in the quadrant, 0 otherwise
q1 = (data(:,3) <= 90);
q2 = (data(:,3) > 90) .* (data(:,3) <= 180);
q3 = (data(:,3) > 180) .* (data(:,3) <= 270);
q4 = (data(:,3) > 271) .* (data(:,3) <= 360);
hold on;
% See DOC QUIVER
% Use QUIVER to specify the start point (tail of the arrow) and direction based on angle
% q1, q2, q3, and q4 are used to generate four different QUIVER handles (h1, h2, h3, and h4)
% This is necessary for varying colors based on direction
% Based on equations: x = x0 + r*cos(theta), y = y0 + r*sin(theta)
% In the usage below, x0 = data(:,1), y0 = data(:,2), theta = data(:,3) * pi / 180
% Can also specify a scale factor as the last argument to quiver (not specified below)
h1 = quiver(data(q1 == 1,1), data(q1 == 1,2), cos(data(q1 == 1,3) * pi/180), sin(data(q1 == 1,3) * pi/180));
h2 = quiver(data(q2 == 1,1), data(q2 == 1,2), cos(data(q2 == 1,3) * pi/180), sin(data(q2 == 1,3) * pi/180)); % sin is negative in 2nd quadrant
h3 = quiver(data(q3 == 1,1), data(q3 == 1,2), cos(data(q3 == 1,3) * pi/180), sin(data(q3 == 1,3) * pi/180));
h4 = quiver(data(q4 == 1,1), data(q4 == 1,2), cos(data(q4 == 1,3) * pi/180), sin(data(q4 == 1,3) * pi/180)); % cos is negative in 4th quadrant
% Set colors to red for 1st quadrant, blue for 2nd, green for 3rd, cyan for 4th
% Also, turn scaling off. get(h1) will return additional property-value pairs
set(h1, 'Color', [1 0 0], 'AutoScale', 'off')
set(h2, 'Color', [0 1 0], 'AutoScale', 'off')
set(h3, 'Color', [0 0 1], 'AutoScale', 'off')
set(h4, 'Color', [0 1 1], 'AutoScale', 'off')
% Done plotting
hold off;

Walter Roberson
Walter Roberson on 21 Jan 2011
To change from compass direction (in degrees) with north being 0, to standard cartesian angles measured counter-clockwise from y=0, use
planar_angle = mod(90 - compass_angle, 360);

zheng
zheng on 21 Jan 2011
Edited: Walter Roberson on 7 Nov 2018
for i=1:length(data(:,1))
if data(i,3)<=90
data(:,4)=90-data(i,3);
end
if data(i,3)>90 && data(i,3)<=180
data(i,4)=360-(data(i,3)-90);
end
if data(i,3)>180 && data(i,3)<=270
data(i,4)=180+(270-data(i,3));
end
if data(i,3)>270 && data(i,3)<=360
data(i,4)=90+(360-data(i,3));
end
end
q1 = (data(:,4) <= 90);
q2 = (data(:,4) > 90) .* (data(:,4) <= 180);
q3 = (data(:,4) > 180) .* (data(:,4) <= 270);
q4 = (data(:,4) > 271) .* (data(:,4) <= 360);
hold on;
h1 = quiver(data(q1 == 1,1), data(q1 == 1,2), cos(data(q1 == 1,3) * pi/180), sin(data(q1 == 1,3) * pi/180));
h2 = quiver(data(q2 == 1,1), data(q2 == 1,2), cos(data(q2 == 1,3) * pi/180), sin(data(q2 == 1,3) * pi/180)); % sin is negative in 2nd quadrant
h3 = quiver(data(q3 == 1,1), data(q3 == 1,2), cos(data(q3 == 1,3) * pi/180), sin(data(q3 == 1,3) * pi/180));
h4 = quiver(data(q4 == 1,1), data(q4 == 1,2), cos(data(q4 == 1,3) * pi/180), sin(data(q4 == 1,3) * pi/180)); % cos is negative in 4th quadrant
could you please explain what does data(q3 == 1,1) mean?
I used if end to convert my azimuth angel to the angle measurend from x = 0 ( as used in this sample code). But it doesnt work. the sample code conside my azimuth angle as the angle measured in contourclockwised from x =0; tks alot

Walter Roberson
Walter Roberson on 21 Jan 2011
The q variables are logical vectors indicating whether each angle is in a particular quadrant. data(q3==1,1) selects only the rows of data that are in quadrant 3, and selects column 1 from the resulting array.
Instead of modifying the existing code to change the logic, go back to the original logic, but copy the assignment of those constants to data into a new variable planar_data, then comment out the assignment to data; then in the planar_data array, set the third column to your sample compass degrees that you want for testing purposes. After that step, assign
data = [planar_data(:,1,2) mod(90 -planar_data(:,3),360)];
Once you have the demonstration going, make it in to a function that takes planar_data as a parameter instead of assigning hard-coded values to it.

Ankita Misra
Ankita Misra on 8 Nov 2017
I have Landsat solar angles which i would like to plot as arrows . I have three variables x,y and the angle . How do I plot them to show meaningful directions ?
  1 Comment
Walter Roberson
Walter Roberson on 9 Nov 2017
I would suggest using quiver(). You could use sin(angle) and cos(angle) as the u and v (or is it the other way around?)

Sign in to comment.


theodore panagos
theodore panagos on 7 Nov 2018
Edited: Walter Roberson on 7 Nov 2018
The formula f(E,N) give an angle that starts from the north,is clockwise,runs from 0 to 360 degrees
and is used in surveying.
f(E,N)=180/pi()*(pi()-pi()/2*(1+sign(N))*(1-sign(E^2))-pi()/4*(2+sign(N))*sign(E) -sign(N*E)*atan((abs(N)-abs(E))/(abs(N)+abs(E))))
There is N(northing)=N2-N1 and E(easting)=E2-E1 ,the formula works for any value of N and E and the result for N=E=0 is undefined.

Community Treasure Hunt

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

Start Hunting!

Translated by