Effacer les filtres
Effacer les filtres

Custom colorbar with predefined ticks

126 vues (au cours des 30 derniers jours)
Ash Ahamed
Ash Ahamed le 12 Nov 2021
Commenté : Ash Ahamed le 12 Nov 2021
Hello! I am trying to create a custom colorbar on a countourf plot using the code below.
grid on
colormap(jet(15))
colorbar
c = colorbar
c.FontSize = 12
caxis([0 25.0])
c.Ticks = [0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 5.0 10.0 15.0 20.0 25.0]
With this code I am getting below colorbar, where as what I wanted is as shown on the right side. I want the colormap on the surface plot to match the colorbar as well. What am I doing wrong here? any advice are greatly appreciated.

Réponse acceptée

DGM
DGM le 12 Nov 2021
Modifié(e) : DGM le 12 Nov 2021
The answer I gave in your last question addressed this issue in part (at least for linear scales). For an arbitrary scale, you can just modify the example
% the given colormap from the last question
cmap = [0.851 0.851 0.851;0.498 1 1;0.298 0.8 1;0 0.6196 1;0 0 0.9529;0.498 1 0.498;0.09804 1 0.09804;0.09804 0.6 0.09804;1 0.749 1;1 0.4 1;0.8 0.09804 1;1 1 0;1 0.651 0;0.9529 0.298 0;0.8 0 0];
% dummy data
x = linspace(0,1,100);
y = x.';
z = max(x,y);
% plot setup
contourf(x,y,z,size(cmap,1)-1)
axis equal
colormap(cmap)
% wrangle the discrete colorbar tick alignment
cb = colorbar;
cbscalein = cb.Limits;
cbscaleout = [min(z(:)) max(z(:))];
ticks = linspace(cbscaleout(1),cbscaleout(2),size(cmap,1)+1);
cb.Ticks = diff(cbscalein)*(ticks-cbscaleout(1))/diff(cbscaleout) + cbscalein(1);
cb.TickLabels = [0:0.1:1 5:5:25]; % this is your arbitrary set of tick values
  1 commentaire
Ash Ahamed
Ash Ahamed le 12 Nov 2021
Thanks! I remembered that and was trying that out. Thanks for the answer and reminder.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Colormaps dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by