I have made a plot for my work and the axis display as
0, 0.1, 0.2, 0., etc
I want to be able to start with 0.0 so the number of decimal places is consistent.

 採用された回答

Kelly Kearney
Kelly Kearney 2013 年 11 月 11 日

2 投票

If you're interested in a shortcut, you can try out this function: tick2text. It cuts out the need to manually grab the tick locations, and can add some more powerful customization too.
plot(rand(10));
tick2text('axis', 'y', 'yformat', '%.1f')

1 件のコメント

dpb
dpb 2013 年 11 月 11 日
That's kewl...are you listening, TMW?

サインインしてコメントする。

その他の回答 (2 件)

dpb
dpb 2013 年 11 月 11 日
編集済み: dpb 2013 年 11 月 11 日

6 投票

Yeah, isn't that annoyingly ugly and klunky??? I first complained to TMW 20-some yr ago and they've not fixed it yet.
Anyway, to patch it you have to fetch the tick values then format and set the tick labels manually 'cuz there's no 'format' property for the tick labels, either. :(
So, diatribes aside...
tix=get(gca,'xtick')';
set(gca,'xticklabel',num2str(tix,'%.1f'))
If you can keep parens straight you can nest the calls to avoid the temporary.
set(gca,'xticklabel',num2str(get(gca,'xtick')','%.1f'))

3 件のコメント

Jonathan
Jonathan 2013 年 11 月 11 日
Thanks for that, I've been trying to find a solution to this for ages.
Philippe Corner
Philippe Corner 2018 年 8 月 3 日
Hi dpb, this would show the number of decimals we need but it changes for example from:
6.5 x10^5 to 6.5000 x10^5
How could we change from
6.5 x10^5 to 650000?
Thanks in advance!
dpb
dpb 2018 年 8 月 3 日
You'll note that Answer came from 2013 and with the pace of change in Matlab that's like back before The Renaissance.
Now you use the numeric ruler properties numeric ruler properties. In particular, set
hAx = gca; % handle to current axes
hAx.YAxis.Exponent=0; % don't use exponent
hAx.YAxis.Format='%d'; % use integer format, no decimal points

サインインしてコメントする。

LaurenN
LaurenN 2018 年 1 月 9 日

5 投票

Just an update I came across, you can set the format of ticks using 'ytickformat' or 'xtickformat' in newer versions of Matlab (looks to be since R2016b). For instance, to achieve the format requested for the y axis:
ytickformat('%.1f')
Further info from the documentation: Change tick marks and tick labels

3 件のコメント

Ameer Hamza
Ameer Hamza 2018 年 4 月 11 日
Great...it works
Walter Roberson
Walter Roberson 2018 年 4 月 11 日
編集済み: Walter Roberson 2018 年 4 月 11 日
For R2014b and later you can
ax = gca;
ax.YRuler.TickLabelFormat = '%.1f';
dpb
dpb 2018 年 4 月 11 日
Now if they would just fix so out-of-box plot axes aren't so ugly...hmmm; have to see if there is a default property; hadn't thought to look after this update.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

質問済み:

2013 年 11 月 11 日

コメント済み:

dpb
2018 年 8 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by