views:

4344

answers:

6

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?

+3  A: 

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
Martijn
@Martijn hi! axes(); overrides my original axes, so I am not using that. the last line does generate the required axis when run individually, but it doesn't overlay the axis on the original plot even with hold on; Any idea what the problem might be??
Lazer
This is strange. It does in my version of matlab (7.6.0.324 (R2008a)) (you can retrieve version number with the version command).If you want to place the x-axis somewhere in the middle of the picture, this is not possible in my version: the x-axis is either at the top or at the bottom (you can set this with the "XAxisLocation" property).
Martijn
The problem I found is that the `hold on;` initializes one set of axes, then the `axes();` command creates a second on top of the first, and this second one becomes the current axes and is not "held on". A simple `plot(...); hold on; plot(...);` order should work.
gnovice
Perhaps Martijn meant to use **axis on** instead of axes
Amro
@Amro: that's also a possibility. I'm not fully aware of the differences between the two; still, the axes are drawn in a 'boxed' style
Martijn
+1  A: 

Maybe grid on will suffice.

Mikhail
grid on is ok (lets me see the intersections), but axes would be better!
Lazer
+1  A: 

@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
Amro
Actually, the problem was the `axes();` call Martijn made (see my comment above). If you have `hold on; plot(x,y); plot(...);` it will still work correctly.
gnovice
I guess you're right, its just that calling hold on before plotting anything will open an empty figure (with default axes) then overwritten by the plot. One the other hand, calling it after plotting something makes more sense (hold the current plot)
Amro
@Amro: True, it is more intuitive to have the hold command following the first plot command.
gnovice
A: 

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
Luisa
A: 

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.

Matt Mizumi
A: 

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.

bta