How do I change the number of decimals in the axis ticks?

142 ビュー (過去 30 日間)
Jimmy Lovén
Jimmy Lovén 2016 年 2 月 29 日
コメント済み: Paul Wintz 2022 年 7 月 2 日
When plotting, the number of decimals in the axis ticks is set by default, and they vary depending on the number, like in the y-axis in the picture:
I want to have them set to 2 by default, so that it will look like this:
For now, I have solved it in a bad way by stating
ax.YTickLabel={'-0.15','-0.10','-0.05','0.00','0.05','0.10','0.15','0.20','0.25','0.30'};
in other words, the ticks are dumb and will not change if the values of the axis change. There must be a better way of handeling this so that, irrespective of the values there will be a fixed set of decimals?

採用された回答

Walter Roberson
Walter Roberson 2016 年 2 月 29 日
ax.YAxis.TickLabelFormat = '%.2f';
Note: this facility is quite new. It was named something different and hidden in R2014b; I do not recall whether it was named this in R2015a or R2015b.
  3 件のコメント
Walter Roberson
Walter Roberson 2018 年 8 月 3 日
If you do not want any decimal places shown, then
ax.YAxis.TickLabelFormat = '%d';
Paul Wintz
Paul Wintz 2022 年 7 月 2 日
Just to clarify, using
ax.YAxis.TickLabelFormat = '%d';
doesn't remove non-integer tick marks. You simply end up with each tick label rounded to the nearest integer.

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

その他の回答 (1 件)

Sergio Yanez-Pagans
Sergio Yanez-Pagans 2021 年 4 月 1 日
This might be more useful given that it only shows relevant ticks and labels (it doesn't only change the format of the label). You'll need to have at least MATLAB 2016:
n_dig = 2 % number of significant digits you want
ctick = get(gca, 'xTick');
xticks(unique(round(ctick,n_dig)));
Hope this is useful!
  1 件のコメント
Walter Roberson
Walter Roberson 2021 年 4 月 1 日
This will not help with the problem being asked about.
By default, the labeling omits trailing 0's, so .30 would be labeled with .3 and .35 would be labeled with .35 . The question was about how to have them both show up with the same number of decimal places -- so as .30 and .35 .

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

カテゴリ

Help Center および File ExchangeAxis Labels についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by