可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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:
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.
回答3:
This should work in Matlab:
set(gca, 'XAxisLocation', 'origin')
Options are: bottom, top, origin.
For Y.axis:
YAxisLocation; left, right, origin
回答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.