Legend & Labeling points in a plot
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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:')
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
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'})
Thanks.
0 Kommentare
Akzeptierte Antwort
Matt J
am 28 Mai 2025
Bearbeitet: Matt J
am 28 Mai 2025
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:')
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
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 Kommentare
Matt J
am 28 Mai 2025
Yes, you can provide whatever labels you want. From the help doc,
% Hout=scatlabel(H,labels) %Provided labels can be string or numeric
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Calculus finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!