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

2019-03-24 16:17发布

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:alt text

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

9条回答
家丑人穷心不美
2楼-- · 2019-03-24 16:54

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.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-03-24 16:56

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.

查看更多
SAY GOODBYE
4楼-- · 2019-03-24 16:56

Maybe grid on will suffice.

查看更多
一夜七次
5楼-- · 2019-03-24 17:02

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.

查看更多
爱情/是我丢掉的垃圾
6楼-- · 2019-03-24 17:05

@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
查看更多
Juvenile、少年°
7楼-- · 2019-03-24 17:06

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
查看更多
登录 后发表回答