MATLAB does not have any built-in functionality for centering the coordinate axes around the origin. This tool is a first fix for solving this. An example script is included showing how to use the function.
Tested on MATLAB 2007b. Works fine, as long as the figure is not resized after the function is called.
Johan Carlson (2021). Centered coordinate axes in 2D plots (https://www.mathworks.com/matlabcentral/fileexchange/22333-centered-coordinate-axes-in-2d-plots), MATLAB Central File Exchange. Retrieved .
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Create scripts with code, output, and formatted text in a single executable document.
thanks a lot for this!
Thanks! I have included your suggested fix, and it appears to be working in the latest release as well (2015b). Will upload the new version in a minute (ver. 1.2).
if the 'Units' property of the figure is fixed as "normalized" there is a problem in the arrow size.
Here is an apparent fix (after definition of figpos):
if (strcmp(get(gcf,'Units'),'normalized'))
screensize = get( 0, 'ScreenSize' );
figpos = figpos.*screensize;
end
Glad you liked it! Never tried doing it for 3D, but in principle you should be able to hack the code to do it.
This is a great bit of code and just what I needed.
Is there a simple way to make this work in 3D?
Mario,
I deliberately removed the end tick labels in on the axes, simply because I didn't think it looked so good. It appeared that changing this takes a bit of fixing of the code to make the arrowheads appear in the right place. Anyway, I'm on it, and I'll post a new version in a couple of days.
/JC
Thank u Johan, I downloaded "centeraxes", it solved my problem.
How do I get the axes ticked from -1 to 1 please? now it's ticked only from -0.9 to 0.9.
Thanks again
Mario
Thank you John!
The option of where to put the center is a simple fix, and I'll definitely deal with that. The other issue is more serious, and unfortunately slightly harder to solve. Nevertheless, I'll give it a go.
Another limitation at the moment is that the tick marks and arrowheads tend to scale if the image is resized after the call to centeraxes.
Anyway, thanks for the feedback.
/Johan
Pretty decent. Doesn't work in cases where you have multiple axes, as you sometimes get with
plot(foo)
hold on
plot(bar)
plot(baz)
hold off
But otherwise, it works pretty darned well. One suggested improvement: allow an option for what "center" to use, so that I could have x- and y-axes intersecting at (-1, 2), if I so chose.