How to make horizontal and vertical axis in matlab plot?
Mostrar comentarios más antiguos
I would like to make a plot which includes the x-axis and the y-axis. So x=0 and y=0. What is the most easy way to do this? Now I have this:
syms x y;
x=-4:4;
y=90-15*x;
plot(x,y);
grid on;
xlabel('x');
ylabel('y');
Respuestas (2)
Try this:
% Method #1:
figure;
x = -4:11;
y = 90 - 15 * x;
plot(x, y, 'b-', 'LineWidth', 2);
grid on;
xlabel('x');
ylabel('y');
% Place black lines along axes:
xline(0, 'Color', 'k', 'LineWidth', 2); % Draw line for Y axis.
yline(0, 'Color', 'k', 'LineWidth', 2); % Draw line for X axis.
% Method #2
figure;
plot(x, y, 'b-', 'LineWidth', 2);
grid on;
xlabel('x');
ylabel('y');
ax = gca;
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
Image Analyst
el 31 de Mayo de 2016
Editada: Image Analyst
el 24 de Sept. de 2023
Try adding this:
grid on;
line([0,0], ylim, 'Color', 'k', 'LineWidth', 2); % Draw line for Y axis.
line(xlim, [0,0], 'Color', 'k', 'LineWidth', 2); % Draw line for X axis.
It doesn't move the tick marks and tick labels for you but it will draw thick black lines where the axes are so that they don't look just like any other grid line.
4 comentarios
Lisanne Hogeveen
el 31 de Mayo de 2016
Editada: Image Analyst
el 24 de Sept. de 2023
Image Analyst
el 31 de Mayo de 2016
Editada: Image Analyst
el 24 de Sept. de 2023
Thanks for accepting!
I wish there were some option to have the tick marks and labels show up along the actual axes instead of the edge of the whole plot/graph, but I don't know of an easy way to do that, short of using text() to draw all the labels yourself. If there is, someone please tell us.
[UPDATE] - there is now. See my other answer.
Steven Lord
el 24 de Sept. de 2023
Should we switch to make your other answer the Accepted answer instead of this one?
Image Analyst
el 25 de Sept. de 2023
@Steven Lord Probably, since most people now will be using modern versions where they can do what I did there in the newer Answer. I think they were unavailable way back in 2016 when I originally answered the question. I unaccepted the old answer but I can't accept my own, new answer so it would be great if you do it. Thanks. 🙂
Categorías
Más información sobre Annotations en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

