Plot a vector with a given angle against the y-axis

19 vues (au cours des 30 derniers jours)
Alex
Alex le 20 Avr 2024
Modifié(e) : Star Strider le 20 Avr 2024
Hi, so as the title states I want to simply plot a line that will make an angle I already know with the y axis, as this line will be plotted in the same figure as other graphs I want it to also be scaled such that it can be seen in these figures as the order of magnitude changes. I know that the vector will have the slope k = sin(angle)/cos(angle) (as it will always intercept the origin) but how do I plot it so that its range is covered by the whole figure. I have attached a figure that shows an example of what I'm trying to plot

Réponses (2)

Paul
Paul le 20 Avr 2024
Modifié(e) : Paul le 20 Avr 2024
Something like this? Note that the line won't won't resize if the axes are resized. If you want something like that, you might be able to use a combination of xline and hgtransform, but I didn't try it.
rng(100);
figure;scatter(randn(100,1),randn(100,1))
theta = 30;
yl = ylim;
line([yl(1)*tand(30) yl(2)*tand(30)],[yl(1) yl(2)])

Star Strider
Star Strider le 20 Avr 2024
Modifié(e) : Star Strider le 20 Avr 2024
Another approach —
thetad = 280; % Desired Angle (Guessing Value)
x = randn(100,1)*2;
y = randn(100,1);
% thetad = 360*rand
arcang = linspace(mod(thetad, 180), 90, 25);
arc = @(yv) [(max(yv)/2)*cosd(arcang); (max(yv)/2)*sind(arcang)];
figure
scatter(x, y, 'x')
hold on
plot(xlim*cosd(thetad), xlim*sind(thetad), '-b', 'LineWidth',2)
arcs = arc(ylim);
plot(arcs(1,:), arcs(2,:), '-b', 'LineWidth',2)
hold off
axis('equal')
title('Normal Ticks, Tick Labels, & Axes, With Solid Lines For Axes')
Ax = gca;
Ax.XAxisLocation = 'origin';
Ax.YAxisLocation = 'origin';
text(median(arcs(1,:)), median(arcs(2,:)), '\theta', 'Horiz','center', 'Vert','bottom', 'Color','b', 'FontSize',16)%, 'FontWeight','bold')
% get(Ax)
figure
scatter(x, y, 'x')
hold on
plot(xlim*cosd(thetad), xlim*sind(thetad), '-b', 'LineWidth',2)
arcs = arc(ylim);
plot(arcs(1,:), arcs(2,:), '-b', 'LineWidth',2)
hold off
axis('equal')
xline(0, '--k', 'LineWidth',2) % Draw Y-Axis
yline(0, '--k', 'LineWidth',2) % Draw X-Axis
title('Invisible Itcks, Tick Labels, & Axes, With Dashed Lines For Axes')
Ax = gca;
Ax.XAxis.Visible = 0;
Ax.YAxis.Visible = 0;
text(median(arcs(1,:)), median(arcs(2,:)), '\theta', 'Horiz','center', 'Vert','bottom', 'Color','b', 'FontSize',16)%, 'FontWeight','bold')
EDIT — (20 Apr 2024 at 19:14)
Made ‘arcang’ more robust.
.

Catégories

En savoir plus sur Graphics dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by