plotting x y cartesian coordinates

조회 수: 112(최근 30일)
Damian Tomaszewski
Damian Tomaszewski 2022년 1월 20일
답변: Image Analyst 2022년 1월 20일
Hello,
I have and issue with poltting a map of points on cartesian plane by using plot function.
I have two sets of data x which is a vector of x coordinates in cartesian plane and y which is a vector of y coordinates in cartesian plane.
f.e
x = [0.00134476655097517 0.00134476655097517 0.00134476655097517
0.0013073145485903 0.00127645748949186 0.00124281266824533
0.00124281266824533 0.00121842112126059 0.00121842112126059
0.00121842112126059 0.00121842112126059 0.00121441160849494]
x = 4×3
0.0013 0.0013 0.0013 0.0013 0.0013 0.0012 0.0012 0.0012 0.0012 0.0012 0.0012 0.0012
y = [0.343050934411873 0.343050934411873 0.343050934411873
0.333496901108166 0.325625242678495 0.317042424078219
0.317042424078219 0.310820122535397 0.310820122535397
0.310820122535397 0.310820122535397 0.309797292885303]
y = 4×3
0.3431 0.3431 0.3431 0.3335 0.3256 0.3170 0.3170 0.3108 0.3108 0.3108 0.3108 0.3098
plot(x,y)
when I'm trying to plot the values are automatically segregated in rising order which in result generates linear function like graph.
The coordinates are x and y are calculated based on a proximity sensor data which is mounted on a stepper motor and scans the enviroment around the center of rotation point returning the distance to the nearest obstacle. Basing on the distance data and the angle which the motor is rotating witch each measurement we can calculate the coordiates in cartesian plane:
I should be able to plot an enviroment map but instead I'm getting a straight line. Where is the problem?

답변(2개)

DGM
DGM 2022년 1월 20일
This isn't the core of the problem, but the way you wrote the x and y vectors causes them to be stored as 4x3 matrices. When you feed them to plot(), they will be plotted as one series per column.
x = [0.00134476655097517 0.00134476655097517 0.00134476655097517 % implicit ;
0.0013073145485903 0.00127645748949186 0.00124281266824533
0.00124281266824533 0.00121842112126059 0.00121842112126059
0.00121842112126059 0.00121842112126059 0.00121441160849494];
y = [0.343050934411873 0.343050934411873 0.343050934411873
0.333496901108166 0.325625242678495 0.317042424078219
0.317042424078219 0.310820122535397 0.310820122535397
0.310820122535397 0.310820122535397 0.309797292885303];
plot(x,y)
legend
If you want them as vectors, you have to continue the lines like so:
x = [0.00134476655097517 0.00134476655097517 0.00134476655097517 ... % continue the same row
0.0013073145485903 0.00127645748949186 0.00124281266824533 ...
0.00124281266824533 0.00121842112126059 0.00121842112126059 ...
0.00121842112126059 0.00121842112126059 0.00121441160849494]
x = 1×12
0.0013 0.0013 0.0013 0.0013 0.0013 0.0012 0.0012 0.0012 0.0012 0.0012 0.0012 0.0012
y = [0.343050934411873 0.343050934411873 0.343050934411873 ...
0.333496901108166 0.325625242678495 0.317042424078219 ...
0.317042424078219 0.310820122535397 0.310820122535397 ...
0.310820122535397 0.310820122535397 0.309797292885303]
y = 1×12
0.3431 0.3431 0.3431 0.3335 0.3256 0.3170 0.3170 0.3108 0.3108 0.3108 0.3108 0.3098
That said, even with that change they will still be plotted as a straight line because y and x are perfectly proportional. It is a straight line.
y./x
ans = 1×12
255.1007 255.1007 255.1007 255.1007 255.1007 255.1007 255.1007 255.1007 255.1007 255.1007 255.1007 255.1007
Your problem lies somewhere either in how the angle/distance data is being captured, or an unintended mistake in how it's being converted. The fact that both y and x are monotonically decreasing might be a clue.

Image Analyst
Image Analyst 2022년 1월 20일
Those are matrices, not vectors. To turn into a vector and plot, you can use (:) and sort():
x = [0.00134476655097517 0.00134476655097517 0.00134476655097517
0.0013073145485903 0.00127645748949186 0.00124281266824533
0.00124281266824533 0.00121842112126059 0.00121842112126059
0.00121842112126059 0.00121842112126059 0.00121441160849494]
x = 4×3
0.0013 0.0013 0.0013 0.0013 0.0013 0.0012 0.0012 0.0012 0.0012 0.0012 0.0012 0.0012
y = [0.343050934411873 0.343050934411873 0.343050934411873
0.333496901108166 0.325625242678495 0.317042424078219
0.317042424078219 0.310820122535397 0.310820122535397
0.310820122535397 0.310820122535397 0.309797292885303]
y = 4×3
0.3431 0.3431 0.3431 0.3335 0.3256 0.3170 0.3170 0.3108 0.3108 0.3108 0.3108 0.3098
% Turn x from a 2-D matrix into a sorted vector.
[x, sortOrder] = sort(x(:), 'ascend')
x = 12×1
0.0012 0.0012 0.0012 0.0012 0.0012 0.0012 0.0012 0.0013 0.0013 0.0013
sortOrder = 12×1
12 4 7 8 11 3 10 6 2 1
% Make y a vector and sort y the same way so we keep points together.
y = y(:);
y = y(sortOrder);
% Now plot the vectors. y vs. x.
plot(x,y, 'b.-', 'LineWIdth', 2, 'MarkerSize', 30);
grid on;
xlabel('x');
ylabel('y');

Community Treasure Hunt

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

Start Hunting!

Translated by