How to show x and y axes in a MATLAB graph?

时光怂恿深爱的人放手 提交于 2019-12-04 16:51:39

问题


I am drawing a graph using the plot() function, but by default it doesn't show the axes.

How do we enable showing the axes at x=0 and y=0 on the graph?

Actually my graph is something like:

And I want a horizontal line corresponding to y=0. How do I get that?


回答1:


By default, plot does show axes, unless you've modified some settings. Try the following

hold on; % make sure no new plot window is created on every plot command
axes(); % produce plot window with axes
plot(% whatever your plot command is);
plot([0 10], [0 0], 'k-'); % plot the horizontal line



回答2:


This should work in Matlab:

set(gca, 'XAxisLocation', 'origin')

Options are: bottom, top, origin.

For Y.axis:

YAxisLocation; left, right, origin



回答3:


The poor man's solution is to simply graph the lines x=0 and y=0. You can adjust the thickness and color of the lines to differentiate them from the graph.




回答4:


If you want the axes to appear more like a crosshair, instead of along the edges, try axescenter from the Matlab FEX.

EDIT: just noticed this is already pointed out in the link above by Jitse Nielsen.




回答5:


Maybe grid on will suffice.




回答6:


@Martijn your order of function calls is slightly off. Try this instead:

x=-3:0.1:3;
y = x.^3;
plot(x,y), hold on
plot([-3 3], [0 0], 'k:')
hold off



回答7:


I know this is coming a bit late, but a colleague of mine figured something out:

figure, plot ((1:10),cos(rand(1,10))-0.75,'*-')
hold on
plot ((1:10),zeros(1,10),'k+-')
text([1:10]-0.09,ones(1,10).*-0.015,[{'0' '1'  '2' '3' '4' '5' '6' '7' '8' '9'}])
set(gca,'XTick',[], 'XColor',[1 1 1])
box off



回答8:


Inspired by @Luisa's answer, I made a function, axes0

x = linspace(-2,2,101);
plot(x,2*x.^3-3*x+1);
axes0

You can follow the link above to download the function and get more details on usage




回答9:


Easiest solution:

plot([0,0],[0.0], xData, yData);

This creates an invisible line between the points [0,0] to [0,0] and since Matlab wants to include these points it will shows the axis.



来源:https://stackoverflow.com/questions/1490778/how-to-show-x-and-y-axes-in-a-matlab-graph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!