How can I make my tick labels include 4 decimal places

I am trying to make a map over a very small area and three decimals is not enough to distinguish between longitude tick labels on my x axis. How can I tell it to include for decimals on that axis? It automatically includes 4 for my y axis.
figure
imagesc(AN(dlatlim,dlonlim))
set(gca,'Xtick',1:400:length(x_label2),'XTickLabel',x_label(1:400:length(x_label2)));
set(gca,'Ytick',1:400:length(y_label2),'YTickLabel',y_label(1:400:length(y_label2)));
set(gca,'fontsize',16)

Answers (1)

ax = gca;
ax.XAxis.TickLabelFormat = '%.4g';

10 Comments

For some reason this doesn't work. The labels stay as 3 decimals :(
Ah, I see you are using the ticklabels to lie to the user about what the underlying data is. The underlying data is integer -- we can tell from the XTick values.
Anyhow, in your case, you need to adjust how you generate the xlabel and ylabel arrays, and setting the TickLabelFormat is not going to help you.
Have you considered using XData and YData properties with imagesc so as to draw the image in the correct data locations so that you would not have to generate the labels yourself, and so that data cursor would work correctly ?
If you load data.mat (attached) and run the lines below then you should see the plot I am trying to fix
figure
imagesc(AN(dlatlim,dlonlim))
set(gca,'Xtick',1:400:length(x_label2),'XTickLabel',x_label(1:400:length(x_label2)));
set(gca,'Ytick',1:400:length(y_label2),'YTickLabel',y_label(1:400:length(y_label2)));
set(gca,'fontsize',16)
ax = gca;
ax.XAxis.TickLabelFormat = '%.4g';
fig = figure;
ax = axes('Parent', fig)
imagesc(ax, x_label(dlonlim), y_label(dlatlim), AN(dlatlim,dlonlim));
ax.XAxis.TickLabelFormat = '%.4g';
ax.YAxis.TickLabelFormat = '%.4g';
Thank you Walter, this does make most of the ticks 4 decimals like I wanted. How would I limit the number of ticks so that I can increase the fontsize on the axes labels? I'm not sure how to translate my lines where I formatted the x and y ticks before since now the tick label information is in the imagesc line.
Also, would this solution work for a figure with multiple subplots where I am trying to add similar axes labels? (see plot attached- same x tick problem)
Thank you so much for your help now and in the past!
%.4f will give exactly 4 decimal places
I realized that for some reason the y axis is flipped over. The latitude values should increase up the y axis and for some reason that is not the case. I'm not sure why plotting the first way (wrong ticks) correctly orients the image but the new way flips the y coordinates...
First way:
figure
imagesc(AN(dlatlim,dlonlim))
set(gca,'Xtick',1:400:length(x_label2),'XTickLabel',x_label(1:400:length(x_label2)));
set(gca,'Ytick',1:400:length(y_label2),'YTickLabel',y_label(1:400:length(y_label2)));
set(gca,'fontsize',16)
New way:
fig = figure;
ax = axes('Parent', fig)
imagesc(ax, x_label(dlonlim), y_label(dlatlim), AN(dlatlim,dlonlim));
ax.XAxis.TickLabelFormat = '%.4f';
ax.YAxis.TickLabelFormat = '%.4f';
set(gca,'fontsize',16)
I think the x axis is also flipped. I'm so confused.
Are x_label or y_label sorted in descending order? If so then that imagesc call would flip the image.
But also some image related operations automatically set YDir property to 'reverse'

Sign in to comment.

Asked:

on 9 Nov 2020

Commented:

on 10 Nov 2020

Community Treasure Hunt

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

Start Hunting!