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

9

15

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?

Godfrey answered 29/9, 2009 at 5:55 Comment(4)
@eSKay: Please can you provide some sample code for how you've made your graph. plot() should show axes, so it would be interesting to see what you've done to make them not show.Amadis
Does #1466998 help? If not, how does what you want differ from what is asked there?Virgilio
@Richie Cotton x and y are arrays. my code is just plot(y,x);Godfrey
@Jitse Niesen thanks for the link.Godfrey
O
11

This should work in Matlab:

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

Options are: bottom, top, origin.

For Y.axis:

YAxisLocation; left, right, origin
Outnumber answered 1/4, 2016 at 0:25 Comment(0)
P
9

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
Partisan answered 29/9, 2009 at 6:28 Comment(5)
@Partisan 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??Godfrey
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).Partisan
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.Despair
Perhaps Martijn meant to use axis on instead of axesPerfectible
@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' stylePartisan
K
5

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.

Kinghorn answered 2/8, 2010 at 21:58 Comment(1)
this does what he needs without a dependency but requires a could lines of code: hold on;plot([0 0],ylim,'k');hold on;plot(xlim,[0 0],'k'); my code doesn't draw tick marks for the axis tho...Seton
D
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.

Disapprove answered 2/8, 2010 at 21:34 Comment(1)
this does what he needs best to his description but requires an extra dependency.Seton
C
3

Maybe grid on will suffice.

Career answered 29/9, 2009 at 6:29 Comment(1)
grid on is ok (lets me see the intersections), but axes would be better!Godfrey
A
3

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
Accelerate answered 31/5, 2010 at 17:36 Comment(1)
Nice idea, I turned it into a function: mathworks.com/matlabcentral/fileexchange/54326-axes0-varargin-Romain
P
2

@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
Perfectible answered 29/9, 2009 at 18:28 Comment(3)
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.Despair
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)Perfectible
@Amro: True, it is more intuitive to have the hold command following the first plot command.Despair
R
1

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

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

Example output for axes0

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

Romain answered 4/12, 2015 at 21:57 Comment(0)
B
-1

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.

Biflagellate answered 15/11, 2014 at 10:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.