Change Number of Decimal Places on Y-Axis Tick Labels on Scatter Plot

6 views (last 30 days)
I am producing a series of scatter plots for a scientific publication and I am trying to change the number of decimal places associated with the tick labels on the y-axis, so that it displays 4 at each increment. Lines of code of interest given below.
ylim([-0.4500 -0.4300])
ytix = get(gca,'ytick')
set(gca,'yticklabel',sprintf('%.4f',ytix));
The values of y-axis tick labels are now indeed include the desired 4 decimal places, but when trying to set the new labels, the entire matrix appears at each tick mark, instead of the appropriate single value. A screenshot of the plot is included.
Any help on how to solve the problem of getting only the right displayed at each tick mark would be appreciated.
-AmericanExpat26

Answers (1)

Star Strider
Star Strider on 10 Sep 2016
You need to create a cell array for the y-tick labels, and split each into a separate element of the cell array. Add a separate assignment to generate the labels, and then write all but the last label (which is blank) to 'YTickLabel':
ytixlbl = regexp(sprintf('%.4f\n',ytix), '\n', 'split');
set(gca,'yticklabel',ytixlbl(1:end-1))
You can do the same thing with the strsplit function as with the regexp call, but not everyone has strsplit.

Community Treasure Hunt

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

Start Hunting!