How do I change the number of decimals in the axis ticks?
조회 수: 375(최근 30일)
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
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 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.
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');
Hope this is useful!