Crossing axis and labels in matlab
Asked Answered
N

2

6

I just can't find it. How to set up axis and labels in matlab so they cross at zero point, with the labels just below the axis not on left/bottom of the plot ?

If I didn't make myself clear - I just want the plot to look like like we all used to draw it when in school. Axes crossing, 4 quadrants, labels right below axis, curve ... as it goes.

Anyone knows how to set it up ?

Nilsson answered 23/9, 2009 at 14:48 Comment(2)
To clarify, Idigas wants the plot to look like upload.wikimedia.org/wikipedia/commons/3/37/… (I think).Nosey
@Jitse Niesen - exactly. While I was searching for the image, you already found it.Nilsson
R
7

You should check out two submissions on The MathWorks File Exchange:

Hopefully these will work with whatever MATLAB version you have (the submission from Matt Fig is the most recently updated one).

Radiolocation answered 23/9, 2009 at 15:6 Comment(4)
@Idigas: I understand what you want now. I updated my answer accordingly.Radiolocation
Hmm, yes I found the first one (the image "upstairs" is from it. But I've been having problems with it when manipulating it.). They both seem like workarounds to me ... do you mean to tell me matlab doesn't support something like this normally ? ... checking out the second one.Nilsson
@Idigas: There's nothing like this currently built-in to MATLAB. You either have to create this sort of thing yourself or find a FEX submission from someone who already has. ;)Radiolocation
Good to know. Anyway, I managed to get the second one working, so that will have to do for now. Appreciate the help in clearing this out for me.Nilsson
T
3

As of Matlab release R2015b, this can be achieved with the axis property XAxisLocation and YAxisLocation being set to origin.

In other words,

x = linspace(-5,5);
y = sin(x);
plot(x,y)

ax = gca;
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';

Axes through origin, from Matlab official documentation

Example is taken from MATLAB official documentation:

Teston answered 1/6, 2017 at 23:29 Comment(1)
+1, one thing to note, it only works for 2D views. One linked question asks about 3D axes, in which case the above properties have no effect.Nysa

© 2022 - 2024 — McMap. All rights reserved.