Move positive and negative y ticks in plot in alignment

In my plot below I want to align the yticks to the right so that the numbers are aligned vertically. The -3 and -6 are closer to the axis line as they have a minus first whereas the 0, 3 and 6 have a space after them. I tried putting a space prior to these three digits so the numbers are aligned but didn't work. Is there a way to do this or make these ticks alig to the right rather than left?
ylim([-6.3 6.3])
yticks([-6 -3 0 3 6]);

 Accepted Answer

Experiment with the TickLabelFormat property of the NumericRuler Properties.
figure
plot(1:10, randn(1,10))
ylim([-6.3 6.3])
yticks([-6 -3 0 3 6]);
Ax = gca;
Ax.YAxis.TickLabelFormat = '%3.0f';
.

6 Comments

Thanks. I've been experimenting with this approach. When I run this without inputting for Ax.YAxis.TickLabelFormat I get the below value. I changed the f to g. When I put in values I don't get any change in the plot. I tried variations on this e.g. Ax.YAxis.TickLabelFormat = '%003.0g';
It works with a figure like you generated above but not this. I generated mine via a GUI but have been able to maniuplate other factors in it such as lables, lines etc. so not sure how to troubleshoot this
K>> Ax.YAxis.TickLabelFormat
ans =
'%g'
It works with a figure like you generated above but not this.
Note that ‘this’ apparently does not refer to anything that I can determine.
The ‘g’ format is not the same as the ‘f’ format. To get results such as I got, use either ‘f’, or for pure integer values, ‘d’, with a width descriptor as well as a format type.
I have very limited (an no recent) experience with GUIs since I have no need to create them, so I can’t help with that part of it.
Thanks I attach a copy of the figure. I tried different variations of the letters e, g, and the nimbers but no differences. When you open figure it might appear grey but if you run below code it should appear.
set(gca,'Units','normalized','OuterPosition',[0 0 1 1])
I can’t do anything with that file. It loads, however I can’t get an axis handle or figure handle for it, so changing it in any way is impossible.
I need something I can work with.
It also crashed MATLAB.
I fixed this thanks.
Another way to get a handle is;
ytickformat('%3.0f');
My pleasure!
That was essentially my original approach. There are several ways to set the tick format, and I chose one that I was certain would work.
If my Answer helped you solve your problem, please Accept it!
.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!