Giving step size to colorbar

Hi all.
I was wondering whether it is possible to give step size to colorbar while plotting using pcolor(X,Y,C) where (X,Y) are the X, Y axes of the plot and C corresponds to the colorbar variable. I used caxis([1e10 1e12]) to set the limits of my colorbar and the result is as in figure 1.png. However, do notice that the colorbar is covered (mostly) entirely by values between 1e11 and 1e12 with only a small portion between 1e10 and 1e11. I however need something like in figure 2.png where the spacing between 1e10 and 1e11 is the same as 1e11 and 1e12.
Any lead would be highly appreciated.
Thank you.

 Risposta accettata

Voss
Voss il 23 Dic 2021
If you had limits from 1 to 100, you would expect about 90% of the colors to be in the 10 to 100 range and about 10% to be in the 1 to 10 range, right? (Actually 90/99 and 9/99, respectively.) That's the same situation as you have here, except that the limits are scaled up by 1e10.
What you are really after is some sort of log-scale colorbar, which as far as I know, there is no built-in support for (maybe on the file exchange you can find something). What you can try is to use log10(C) for your color values and use log10() of your color limits:
pcolor(X,Y,log10(C));
caxis([10 12]);
colorbar();
then manually set the YTickLabels of the colorbar axes to {'10^{10}', '10^{11}', '10^{12}'}. It may be a little tricky to maintain the correct YTickLabels on the colorbar as the data change, but it is feasible.

1 Commento

Thank you so much for the suggestion. I guess colorbar('XTickLabel', {'10^{10}', '10^{11}', '10^{12}'}, 'XTick',log10(1e10):1:log10(1e12) ); should work based on your answer.
Thanks again.

Accedi per commentare.

Più risposte (1)

You can make "steps" in your colorbar independently of the caxis. Just specify how many rows in your colormap,
cmap = jet(8); % 8 steps.
colormap(cmap);
colorbar;
or
cmap = jet(32); % 32 steps
colormap(cmap);
colorbar;

3 Commenti

Thank you for the answer. Doing this would definitely change the number of steps in the colorbar but the scales would still be the same. For example, if the limits in the colorbar runs from 1e10 -9e10 when the step size is 8, it maintains this limit of 1e10 - 9e10 even if I change the step size to 32.
What I was really looking for was changing this limit shown in the colorbar and hence the distance between points shown in the colorbar - sorry if my content gave a wrong impression. I was looking for something in figure 2.png instead of figure 1.png. Things would have been easy if the numbers in the colorbar were between 1 and 20 (for instance) but what I have is numbers varying in powers of 10. I was looking for equal spacing in the colorbar between 1e10 and 1e11, and that between 1e11 and 1e12, and so on.
Thanks again.
If you're looking to change the number and location the tick marks and their labels in the colorbar, instead of the number of uniform color steps in the colorbar, then you can adapt this example from the help:
contourf(peaks)
colorbar('Ticks',[-5,-2,1,4,7],...
'TickLabels',{'Cold','Cool','Neutral','Warm','Hot'})
Thank you. That is really helpful as well.

Accedi per commentare.

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by