Legend & Labeling points in a plot

I'd like to have a legend that shows all tangent lines and curves of my graph, also label points if possible either in the legend or right next to the points themselves. My code doesn't do either currently:
syms a x
y=1/(x-1); m=-1; dy=diff(y);
x1_=subs(dy,a); eq=x1_==m; x1=solve(eq); y1=subs(y,x1);
line=m*(x-x1)+y1;
disp('The tangent line(s) are:')
The tangent line(s) are:
for k=1:length(x1)
fprintf('y = %s\n',char(line(k)))
f=plot(x1,y1,'.', 'color', 'black','markersize',15); hold on
g=fplot(line); set(g,'color','black','LineStyle', '--'); hold on;
end
y = - x - 1 y = 3 - x
h=fplot(y); set(h,'color','blue','Linewidth', 1);
j=xline(0); set(j,'color','black','Linewidth', 1.5)
k=yline(0); set(k,'color','black','Linewidth', 1.5)
xlabel('x'); ylabel('y'); title('Curve & tangent line & points')
legend([g h],{'Tangent line', 'Curve'})
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
Thanks.

 採用された回答

Matt J
Matt J 2025 年 5 月 28 日
編集済み: Matt J 2025 年 5 月 28 日

1 投票

syms a x
y=1/(x-1); m=-1; dy=diff(y);
x1_=subs(dy,a); eq=x1_==m; x1=solve(eq); y1=subs(y,x1);
line=m*(x-x1)+y1;
disp('The tangent line(s) are:')
The tangent line(s) are:
for k=1:length(x1)
fprintf('y = %s\n',char(line(k)))
f(k)=plot(x1(k),y1(k),'.', 'MarkerSize',15); hold on
g(k)=fplot(line(k),'--');
end
y = - x - 1 y = 3 - x
h=fplot(y,'color','blue','Linewidth', 1);
xline(0,'color','black','Linewidth', 1.5);
yline(0,'color','black','Linewidth', 1.5);
xlabel('x'); ylabel('y'); title('Curve & tangent line & points')
legend([g(:); f(:); h],{'Tangent line 1','Tangent line 2' ,'Point 1','Point 2', 'Curve'})
col={'red','green'};
[f.Color]=deal(col{:});
[g.Color]=deal(col{:});
hold off

6 件のコメント

rezheen
rezheen 2025 年 5 月 28 日
編集済み: rezheen 2025 年 5 月 28 日
Can I state the tangent lines once in the legend since they are the same color?
Also, labeling the points (numerically) would be nice. Thanks for the reply.
Matt J
Matt J 2025 年 5 月 28 日
You can make them different colors if you want. See revised version.
rezheen
rezheen 2025 年 5 月 28 日
編集済み: rezheen 2025 年 5 月 28 日
But the code would be messy say if I changed the curve to:
y=piecewise(-4*pi<=x<=4*pi,tan(x)); m=1;
Too many tangent lines could pop up. I'd run out of colors lol.
Matt J
Matt J 2025 年 5 月 28 日
編集済み: Matt J 2025 年 5 月 28 日
syms a x
y=1/(x-1); m=-1; dy=diff(y);
x1_=subs(dy,a); eq=x1_==m; x1=solve(eq); y1=subs(y,x1);
line=m*(x-x1)+y1;
disp('The tangent line(s) are:')
The tangent line(s) are:
disp(char(compose('y = %s',string(line))))
y = - x - 1 y = 3 - x
scatlabel( scatter(x1,y1,'k.', 'SizeData',150) ); hold on
g=fplot(line,'k--');
h=fplot(y,'color','blue','Linewidth', 1); hold off
xline(0,'color','black','Linewidth', 1.5);
yline(0,'color','black','Linewidth', 1.5);
xlabel('x'); ylabel('y'); title('Curve & tangent line & points')
legend([g(1); h],{'Tangent line', 'Curve'})
function varargout=scatlabel(H,labels)
%Add labels to the (XData,YData,ZData) coordinates in a plot pointed to by a
%graphics handle object H.
%
% Hout=scatlabel(H) %Labels will be simple enumeration
% Hout=scatlabel(H,labels) %Provided labels can be string or numeric
%
%Hout is a handle to a GraphPlot object containing the labels.
ax=H(1).Parent;
S.XData=[H.XData];
S.YData=[H.YData];
for i=numel(H):-1:1
if isprop(H(i),'ZData') && ~isempty(H(i).ZData),
ZData{i}=[H(i).ZData];
else
ZData{i}=zeros(1,numel(H(i).XData));
end
end
S.ZData=cell2mat(ZData);
N=numel(S.XData);
if nargin<2, labels=1:N; end
assert(numel(labels)==N,'Number of labels doesn''t match number of data points');
tf=ishold;
if ~tf
hold(ax,'on')
end
args={'Marker','none','NodeLabel',labels,'NodeFontSize',12,'HitTest','off',...
'XData', S.XData, 'YData', S.YData};
if isfield(S,'ZData'),
args=[args,{'ZData',S.ZData}];
end
H=plot(ax,graph( speye(N),'omit' ),args{:});
if ~tf
hold(ax,'off')
end
if nargout, varargout={H}; end
end
rezheen
rezheen 2025 年 5 月 28 日
That's great! But instead of labeling the points '1', '2', could it label what they actually are?
In this case (0, -1), (2, 1)
Matt J
Matt J 2025 年 5 月 28 日
Yes, you can provide whatever labels you want. From the help doc,
% Hout=scatlabel(H,labels) %Provided labels can be string or numeric

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

製品

リリース

R2022a

質問済み:

2025 年 5 月 28 日

コメント済み:

2025 年 5 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by