How to change the scale of color bar to discrete ?

35 views (last 30 days)
hi,
I have my contour plot like this figure,
I want to change the scale of colorbar to a discrete one with the scale value [ 0, 0.1 ,0.2 ,0.3 ,0.4 ,0.5 ,0.6 ,0.7 ,0.75 ,0.8 ,0.85, 0.9 ,1]
how to do it?

Accepted Answer

Voss
Voss on 15 Oct 2023
You can use a colormap that has duplicate colors wherever the spacing between the levels is 0.1:
x = [-7.22 -6.70 -6.19 -5.67 -5.15 -4.64 -4.12 -3.60 -3.09 -2.56 -2.04 -1.53 -1.00 -0.51 0.02 0.54 1.06 1.58 2.10 2.60 3.12 3.64 4.16 4.69 5.18 5.70 6.23 6.75 7.25];
y = [7.20 6.70 6.18 5.68 5.14 4.62 4.11 3.59 3.09 2.57 2.05 1.54 1.02 0.51 -0.01 -0.53 -1.03 -1.55 -2.07 -2.59 -3.09 -3.61 -4.14 -4.65 -5.16 -5.67 -6.19 -6.71 -7.22];
strain_12 = [0.48 0.64 0.66 0.7 0.68 0.72 0.76 0.78 0.9 0.88 0.86 0.82 0.88 0.84 0.9 0.88 0.9 0.86 0.88 0.82 0.82 0.8 0.8 0.78 0.76 0.72 0.7 0.68 0.64 0.5 0.64 0.66 0.76 0.68 0.76 0.76 0.78 0.82 0.82 0.8 0.8 0.82 0.84 0.88 0.84 0.86 0.84 0.86 0.8 0.78 0.74 0.72 0.74 0.74 0.72 0.72 0.68 0.64 0.54 0.7 0.68 0.76 0.7 0.68 0.7 0.74 0.76 0.72 0.8 0.86 0.84 0.9 0.92 0.86 0.88 0.86 0.86 0.84 0.8 0.76 0.74 0.7 0.7 0.74 0.72 0.7 0.68 0.48 0.68 0.72 0.74 0.72 0.7 0.7 0.76 0.82 0.82 0.86 0.88 0.84 0.88 0.88 0.88 0.86 0.88 0.9 0.94 0.9 0.86 0.82 0.78 0.76 0.76 0.76 0.76 0.7 0.48 0.66 0.7 0.68 0.7 0.72 0.7 0.74 0.76 0.76 0.82 0.88 0.84 0.84 0.86 0.8 0.82 0.84 0.82 0.88 0.88 0.9 0.88 0.82 0.76 0.78 0.74 0.8 0.72 0.48 0.66 0.68 0.66 0.7 0.78 0.8 0.82 0.82 0.8 0.84 0.9 0.92 0.86 0.84 0.8 0.82 0.82 0.82 0.86 0.92 0.9 0.88 0.82 0.76 0.76 0.74 0.78 0.7 0.5 0.62 0.54 0.56 0.64 0.78 0.86 0.82 0.82 0.82 0.78 0.82 0.86 0.82 0.8 0.82 0.82 0.82 0.8 0.82 0.84 0.82 0.78 0.78 0.74 0.72 0.68 0.7 0.64 0.52 0.62 0.54 0.58 0.64 0.76 0.84 0.82 0.8 0.82 0.8 0.82 0.84 0.84 0.84 0.82 0.8 0.78 0.76 0.76 0.76 0.76 0.76 0.72 0.74 0.7 0.68 0.7 0.7 0.48 0.6 0.58 0.62 0.66 0.76 0.86 0.8 0.78 0.8 0.78 0.82 0.84 0.84 0.82 0.82 0.8 0.8 0.78 0.76 0.78 0.78 0.8 0.76 0.74 0.72 0.7 0.72 0.68 0.48 0.62 0.66 0.7 0.74 0.74 0.78 0.72 0.74 0.74 0.74 0.8 0.76 0.78 0.78 0.8 0.76 0.74 0.74 0.78 0.78 0.8 0.78 0.76 0.74 0.78 0.76 0.8 0.72 0.46 0.62 0.7 0.76 0.76 0.78 0.82 0.72 0.76 0.74 0.76 0.78 0.76 0.76 0.82 0.82 0.78 0.76 0.78 0.82 0.78 0.8 0.78 0.8 0.78 0.78 0.78 0.76 0.68 0.44 0.6 0.7 0.72 0.7 0.7 0.68 0.68 0.74 0.72 0.74 0.72 0.72 0.74 0.78 0.82 0.82 0.82 0.8 0.82 0.78 0.82 0.78 0.8 0.74 0.74 0.7 0.7 0.62 0.44 0.64 0.72 0.7 0.7 0.68 0.64 0.72 0.76 0.8 0.8 0.76 0.76 0.78 0.78 0.8 0.8 0.78 0.76 0.76 0.78 0.8 0.78 0.78 0.76 0.72 0.72 0.68 0.64 0.4 0.62 0.7 0.72 0.72 0.68 0.66 0.72 0.72 0.76 0.78 0.74 0.78 0.82 0.8 0.8 0.8 0.78 0.76 0.74 0.8 0.78 0.76 0.74 0.72 0.68 0.68 0.66 0.64 0.42 0.58 0.68 0.72 0.76 0.72 0.7 0.74 0.76 0.76 0.74 0.74 0.74 0.78 0.76 0.78 0.78 0.8 0.78 0.76 0.78 0.74 0.72 0.74 0.7 0.7 0.72 0.7 0.7 0.38 0.52 0.62 0.68 0.7 0.72 0.74 0.8 0.8 0.82 0.76 0.76 0.76 0.76 0.8 0.78 0.76 0.78 0.78 0.76 0.76 0.74 0.76 0.76 0.74 0.76 0.72 0.7 0.68 0.36 0.5 0.62 0.66 0.72 0.78 0.8 0.82 0.78 0.78 0.7 0.74 0.76 0.74 0.8 0.76 0.72 0.78 0.78 0.78 0.8 0.82 0.78 0.76 0.72 0.74 0.72 0.72 0.7 0.38 0.54 0.62 0.64 0.7 0.76 0.74 0.8 0.8 0.8 0.74 0.8 0.8 0.76 0.82 0.76 0.76 0.8 0.82 0.8 0.84 0.86 0.82 0.78 0.74 0.78 0.74 0.72 0.72 0.4 0.6 0.66 0.66 0.7 0.7 0.7 0.76 0.74 0.76 0.74 0.76 0.84 0.82 0.84 0.78 0.8 0.82 0.84 0.8 0.82 0.82 0.76 0.74 0.74 0.78 0.7 0.66 0.66 0.44 0.64 0.7 0.66 0.66 0.64 0.68 0.72 0.7 0.76 0.76 0.78 0.82 0.8 0.8 0.82 0.84 0.82 0.8 0.82 0.8 0.8 0.76 0.74 0.74 0.72 0.7 0.66 0.66 0.46 0.66 0.74 0.72 0.66 0.68 0.7 0.68 0.66 0.72 0.76 0.8 0.86 0.86 0.82 0.84 0.82 0.78 0.78 0.84 0.84 0.78 0.78 0.76 0.74 0.72 0.72 0.64 0.68 0.48 0.68 0.78 0.78 0.72 0.74 0.76 0.74 0.68 0.64 0.72 0.8 0.84 0.9 0.84 0.84 0.8 0.8 0.84 0.88 0.88 0.82 0.76 0.78 0.76 0.68 0.7 0.68 0.7 0.42 0.62 0.74 0.74 0.74 0.76 0.78 0.78 0.8 0.78 0.84 0.86 0.86 0.9 0.86 0.84 0.82 0.8 0.86 0.9 0.92 0.84 0.84 0.82 0.8 0.74 0.74 0.72 0.72 0.36 0.56 0.66 0.72 0.76 0.74 0.8 0.8 0.8 0.78 0.84 0.82 0.8 0.86 0.84 0.8 0.8 0.8 0.84 0.88 0.9 0.86 0.82 0.82 0.8 0.74 0.72 0.72 0.72 0.3 0.48 0.58 0.6 0.64 0.68 0.72 0.74 0.78 0.8 0.8 0.78 0.76 0.82 0.82 0.78 0.78 0.78 0.8 0.84 0.86 0.84 0.82 0.78 0.78 0.74 0.72 0.74 0.72 0.28 0.44 0.58 0.64 0.7 0.72 0.72 0.7 0.72 0.76 0.76 0.82 0.8 0.88 0.84 0.8 0.74 0.74 0.74 0.84 0.82 0.8 0.8 0.8 0.76 0.74 0.68 0.66 0.66 0.32 0.48 0.62 0.66 0.7 0.7 0.72 0.74 0.76 0.76 0.76 0.8 0.8 0.86 0.86 0.84 0.74 0.74 0.74 0.82 0.82 0.82 0.78 0.78 0.8 0.74 0.7 0.66 0.68 0.32 0.5 0.66 0.72 0.74 0.8 0.8 0.8 0.8 0.78 0.8 0.84 0.86 0.88 0.92 0.9 0.84 0.78 0.84 0.9 0.9 0.9 0.84 0.84 0.82 0.78 0.74 0.68 0.66 0.34 0.54 0.7 0.74 0.76 0.8 0.78 0.78 0.8 0.78 0.8 0.82 0.84 0.9 0.96 0.96 0.9 0.84 0.86 0.9 0.86 0.88 0.82 0.82 0.82 0.76 0.76 0.74 0.68];
[a,b] = meshgrid(x,y);
gama_12 = reshape(strain_12,29,29);
shear = rescale(gama_12,0,1);
contourf(b,a,shear);
lvls = [ 0, 0.1 ,0.2 ,0.3 ,0.4 ,0.5 ,0.6 ,0.7 ,0.75 ,0.8 ,0.85, 0.9 ,1];
% find the indices where the level difference is "big" (0.1):
lvl_idx = find(diff(lvls) > 0.075); % (closer to 0.1 than to 0.05)
n_big = numel(lvl_idx);
% create a colormap with an extra color for each big level difference:
cmap = jet(numel(lvls)-1+n_big);
% change indices in lvls to indices in the colormap:
cmap_idx = lvl_idx;
for ii = 1:n_big
cmap_idx(ii) = lvl_idx(ii) + nnz(lvl_idx <= lvl_idx(ii));
end
% duplicate the colors in the colormap at the big level indices:
cmap(cmap_idx,:) = cmap(cmap_idx-1,:);
% apply the colormap, create the colorbar, and set the ticks to lvls:
colormap(cmap);
cb = colorbar();
cb.Ticks = lvls;
  3 Comments
Voss
Voss on 19 Oct 2023
As long as the levels are spaced 0.05 or 0.1 apart, then yes, it should work. More generally, it will work any time you have two distinct level spacings where the larger of which is twice the smaller (so that you can just duplicate certain colors); you just may have to change the 0.075 threshold it's using. More generally than that, if you want arbitrary different spacings, you'd have to adapt the code more significantly since this approach requires you to find the greatest common divisor of the level spacings and repeat different colors different number of times (e.g., the color corresponding to a spacing of 0.2 when the smallest spacing you have is 0.01 would have to be repeated 20 (=0.2/0.01) times in the colormap).
DGM
DGM on 19 Oct 2023
Edited: DGM on 20 Oct 2023
If the level spacing is arbitrary, consider the example here.
Extending that for use with contour() should be easy enough, though to guarantee correct mapping would be a bit more complicated than it is with other types of plots. The issue is similar to the CDataMapping = 'scaled' example in the above link. With contour()/contourf() plots, we're stuck using that.
That said, it should suffice if the generated map is long.
% fake unit-scale data
[X Y] = meshgrid(linspace(0,0.5,500));
Z = X + Y;
% arbitrarily-spaced breakpoints
lvl = [0 0.1 0.2 0.25 exp(-1) 0.4 0.5 2/3 1/sqrt(2) 0.82 1];
% interval/category colors
CT0 = jet(numel(lvl)-1);
% generate expanded color table & info
nmax = 1000; % let this be long
[CT cidx] = intervalct(lvl,CT0,nmax);
% plot everything
contourf(X,Y,Z,lvl)
% add colorbar with matching ticks
colormap(CT);
cb = colorbar;
cb.Ticks = lvl;
caxis(imrange(lvl));
... though beware that using smooth colormaps can make it hard to distinguish discrete levels if there are more than a handful of levels.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 15 Oct 2023
Try this. Adapt as needed:
% Read in sample image.
grayImage = mat2gray(imread('cameraman.tif'));
% Make a colormap with 10 "zones".
% [ 0, 0.1 ,0.2 ,0.3 ,0.4 ,0.5 ,0.6 ,0.7 ,0.75 ,0.8 ,0.85, 0.9 ,1]
cmap = turbo(10)
cmap = 10×3
0.1900 0.0718 0.2322 0.2737 0.3835 0.8449 0.2093 0.6654 0.9760 0.1034 0.8960 0.7150 0.4483 0.9959 0.3694 0.7824 0.9376 0.2033 0.9800 0.7300 0.2216 0.9643 0.4186 0.0964 0.7941 0.1660 0.0143 0.4796 0.0158 0.0106
% Display the gray scale image.
imshow(grayImage);
% Apply the colormap to pseudocolor the image.
colormap(cmap)
% Show a colorbar along the right side of the image.
colorbar

Categories

Find more on Color and Styling in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!