How to draw perpendicular line from scatter points in 2-D plot

14 views (last 30 days)
hold on
x=[0:0.02:2]
y=sin(x.^3 - 2)
plot(x,y,'k')
a=randperm(length(x),10);
b=x(a)
z=sin(b.^3 - 2)
scatter(b,z)
i have used this code to plot a line and get 10 random scatter points on it, now i need to make perpendicular lines from those scatter points onto the x and y axis, how do i do that

Accepted Answer

Adam Danz
Adam Danz on 24 Jun 2021
Edited: Adam Danz on 24 Jun 2021
How to plot horizontal and vertical lines from axes to data point
clf()
ax = gca();
hold(gca,'on')
x=[0:0.02:2];
y=sin(x.^3 - 2);
plot(ax,x,y,'k')
a=randperm(length(x),10);
b=x(a);
z=sin(b.^3 - 2);
scatter(ax,b,z)
xMin = ax.XLim(1);
yMin = ax.YLim(1);
xln = plot(ax, [1;1]*b(:).', [z(:).';yMin*ones(1,numel(z))], 'c--');
yln = plot(ax, [b(:).';xMin*ones(1,numel(z))], [1;1]*z(:).', 'c--');
How to update the reference lines when axis limits change
The solution above will create lines that extend to existing axis limits. If the axis limits change due to user interaction or setting xlim/ylim, the reference line endings will not be updated.
Add the section below to automatically update the reference line endings when the axis limits change. This uses LimitsChangedFcn which was introduced in Matlab R2021a.
ax.XAxis.LimitsChangedFcn = @(axh,data)updateRefLines(data, yln, 'x');
ax.YAxis.LimitsChangedFcn = @(axh,data)updateRefLines(data, xln, 'y');
function updateRefLines(data,h,xy)
% Responds to LimitsChangedFcn by updating the line endpoints in
% h (handles to line objs) to the new axis limits in data.
% Axis that changed is identified by 'xy'.
if strcmpi(xy,'x')
% xlim changed, update the y-data
coordinates = vertcat(h.XData);
coordinates(:,1) = data.NewLimits(1);
set(h, {'XData'}, mat2cell(coordinates,ones(size(h)),2))
else
% ylim changed, update x-data
coordinates = vertcat(h.YData);
coordinates(:,1) = data.NewLimits(1);
set(h, {'YData'}, mat2cell(coordinates,ones(size(h)),2))
end
end
  3 Comments
Adam Danz
Adam Danz on 24 Jun 2021
If you remove those 3 lines you will get an error message stating the xMin and yMin are undefined (after clearing your workspace).
The first line gets the axis handles.
The second and third lines store the minimum axis limits for the x and y axes (ie, the left and bottom limits).
Adam Danz
Adam Danz on 24 Jun 2021
Edited: Adam Danz on 24 Jun 2021
I've updated my answer by moving ax=gca() above your code and by applying the axis handle, ax, to your plotting functions. Specifying axis handles, or 'parents' is best-practice and something you sould adopt as a newbie.
I also agree with John's advice about variable names, of course.
Even better than ax=gca() is to specify the parent of the axes using
fig = figure();
ax = axes(fig);
gca() 'get current axes' should be avoided when possible since it will assign the current axis handle which may not be the axis handle you're working with. By using the 2 lines above, you completely control the objects and where they are assigned.

Sign in to comment.

More Answers (1)

John D'Errico
John D'Errico on 24 Jun 2021
I would STRONGLY recommend you learn to use descriptive variable names. a? b? z?
MATLAB does not charge more for you to use two or, god forbid, even three letter names. Even more are possible, or so I hear. But the use of descriptive variable names will make your code possible to debug, when your code becomes longer and far more complex.
hold on
x=[0:0.02:2];
y=sin(x.^3 - 2);
plot(x,y,'k-')
a=randperm(length(x),10);
b=x(a);
z=sin(b.^3 - 2);
scatter(b,z)
I have no idea why you selected points as you did, as there are far easier ways to generate points randomly from an interval. (Read the help for rand.) But consider what this would do:
my_x = rand(1,10)*2;
But now, you want to generate WHAT perpendicular? Perpendicular lines to the curve? Or do you want to drop line segments from those points that are perpendicular to the x axis? Both are pretty easy, but the latter is trivial.
figure
plot(x,y,'k-')
hold on
scatter(b,z);
plot([b;b],[-ones(size(z));z],'-r')
  2 Comments
Shekhar Gangal
Shekhar Gangal on 24 Jun 2021
how to plot the perpendicular lines like this to the y axis
im new to matlab coding so a lot of this is new to me
Shekhar Gangal
Shekhar Gangal on 24 Jun 2021
nevermind, i figured it out, plus i also realized the imp of xlim and ylim

Sign in to comment.

Categories

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

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!