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

8 次查看(过去 30 天)
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

回答(1 个)

Star Strider
Star Strider 2016-9-10
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.

类别

Help CenterFile Exchange 中查找有关 Scatter Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by