"Are they particularly well suited to coloring 3D shapes/contours (as the name "colorcube" seems to imply)?" No, not particularly. Discrete colormaps are used to color data which are not continuous, e.g. discrete categories or groups or the like. You can search the internet for thousands of tutorials on "discrete vs continuous data". Discrete colormaps are usually defined in terms a fixed set of colors, for example: FLAG (3 colors) PRISM (6 colors) LINES (whatever the current default axes ColorOrder has) The LINES colormap gives a big hint as to what discrete colormaps are good for: they are commonly used for coloring plot lines, scatter plots, and other discrete data which need to be distinguished. The LINES colormaps is defined in terms of the axes ColorOrder, so the colormaps simply gives the user access to the same colors that are used by all commands that use the ColorOrder. Read more here: https://www.mathworks.com/help/matlab/ref/matlab.graphics.axis.axes-properties.html#budumk7_sep_shared-ColorOrder "What are some good examples of non-continuous colormap applications" The following page gives exactly what you asked for, good examples of discrete colormaps in use: https://www.mathworks.com/help/matlab/ref/colororder.html IMHO the (default) colormap LINES is an okay set of colors, whereas FLAG and PRISM are basically useless. Here are some other discrete colormaps: https://www.mathworks.com/matlabcentral/fileexchange/62729-matplotlib-perceptually-uniform-colormaps https://www.mathworks.com/matlabcentral/fileexchange/45208-colorbrewer-attractive-and-distinctive-colormaps Or generate your own set of discrete colors: https://www.mathworks.com/matlabcentral/fileexchange/70215-maximally-distinct-color-generator Answer from Stephen23 on mathworks.com
Top answer
1 of 1
2
"Are they particularly well suited to coloring 3D shapes/contours (as the name "colorcube" seems to imply)?" No, not particularly. Discrete colormaps are used to color data which are not continuous, e.g. discrete categories or groups or the like. You can search the internet for thousands of tutorials on "discrete vs continuous data". Discrete colormaps are usually defined in terms a fixed set of colors, for example: FLAG (3 colors) PRISM (6 colors) LINES (whatever the current default axes ColorOrder has) The LINES colormap gives a big hint as to what discrete colormaps are good for: they are commonly used for coloring plot lines, scatter plots, and other discrete data which need to be distinguished. The LINES colormaps is defined in terms of the axes ColorOrder, so the colormaps simply gives the user access to the same colors that are used by all commands that use the ColorOrder. Read more here: https://www.mathworks.com/help/matlab/ref/matlab.graphics.axis.axes-properties.html#budumk7_sep_shared-ColorOrder "What are some good examples of non-continuous colormap applications" The following page gives exactly what you asked for, good examples of discrete colormaps in use: https://www.mathworks.com/help/matlab/ref/colororder.html IMHO the (default) colormap LINES is an okay set of colors, whereas FLAG and PRISM are basically useless. Here are some other discrete colormaps: https://www.mathworks.com/matlabcentral/fileexchange/62729-matplotlib-perceptually-uniform-colormaps https://www.mathworks.com/matlabcentral/fileexchange/45208-colorbrewer-attractive-and-distinctive-colormaps Or generate your own set of discrete colors: https://www.mathworks.com/matlabcentral/fileexchange/70215-maximally-distinct-color-generator
Discussions

define a sequential discrete color map with first color white.
https://www.mathworks.com/matlabcentral/answers/203750-define-a-sequential-discrete-color-map-with-first-color-white#answer_175523 ... The colormap you uploaded in your original question looks a lot like a Cynthia Brewer colormap, which Stephen Cobeldick has made into a nice little function ... More on mathworks.com
🌐 mathworks.com
3
1
April 15, 2015
How do I make the colorbar of my image plot in matlab to get discrete values without only giving colour to parts of my data - Stack Overflow
I am plotting a matrix with rain intensities with an image plot in matlab. I want the colorbar to show a distinct colour for 0-10, 10-20... to 90-10. I've tried the function surf(10) to get 10 disc... More on stackoverflow.com
🌐 stackoverflow.com
How to have a discrete color bar for scatter3?
How to have a discrete color bar for scatter3?. Learn more about scatter3, color, colorbar, colormap MATLAB More on mathworks.com
🌐 mathworks.com
2
0
March 23, 2016
How to make a discrete colorbar with specified intervals?
https://www.mathworks.com/matlabcentral/answers/738107-how-to-make-a-discrete-colorbar-with-specified-intervals#answer_616702 ... Your colorbar is set by the resolution of your colormap. If you want a colobar with 16 colors, set a colormap that only has 16 defined colors. More on mathworks.com
🌐 mathworks.com
2
4
February 7, 2021
Top answer
1 of 2
5
In re-reading the orginal question, it is worth clarifying that this solution only visually creates the colorbar. On its own that is not enough to make the data in the plot match the colorbar. Note that the tick locations are evenly spaced from 0 to 500. The code programmatically replaces the original labels with the new labels. Separate code is necessary to rescale the values so that the colors in the colorbar correspond to values in the plot. Said another way, anything colored white has a value up to 31.25, not 0.01. tickVals = linspace(0,500,17) Apologies for that confusion. Here's sample code for how I would do this for my example. To work, you also have to specify the levels contourf should use. contourf(___,levels) % Create a data set with values from 0 to 600 (was 500 previously) data = rescale(peaks,0,600); % copy a copy of the data for rescaling newdata = data; % New tick values newTickVals = [0 0.01 0.5 1 1.5 2 5 10 15 20 25 50 75 100 250 500 inf]; for tv = 2:length(newTickVals) % find all data between the new tick ranges, one block at a time ind = data>newTickVals(tv-1) & data<=newTickVals(tv); % Change the corresponding values in the copied data to scale to the % block edges newdata(ind) = rescale(data(ind),tickVals(tv-1),tickVals(tv)); end % The rest of the code remains the same (with addition of specifying % levels) contourf(newdata,tickVals) C=parula(15); C(end+1,:)=1; colormap(flipud(C)) colorbar('Ticks',tickVals,... 'TickLabels',["" "0.01" "0.5" "1" "1.5" "2" "5" "10" "15" "20" "25" "50" "75" "100" "250" "500" ">500"]); Now the colorbar colors correspond to the values in the original data variable Note: Because this approach rescales the data to make a non-linear colorbar range possible, I think this approach only works for 2D plots (i.e. plots where only color is used to show the Z value. This approach will distort the appearance of the plot if it is plotted in 3D, e.g. a surface plot.)
2 of 2
2
Your colorbar is set by the resolution of your colormap. If you want a colobar with 16 colors, set a colormap that only has 16 defined colors. Here's an example that recreates the "block" colorbar. contourf(rescale(peaks,0,500)) C=parula(15); C(end+1,:)=1; colormap(flipud(C)) colorbar('Ticks',linspace(0,500,17),... 'TickLabels',["" "0.01" "0.5" "1" "1.5" "2" "5" "10" "15" "20" "25" "50" "75" "100" "250" "500" ">500"]);
Find elsewhere
🌐
MathWorks
mathworks.com › matlab › graphics › labels and styling › color and styling
colormap - View and set current colormap - MATLAB
This MATLAB function sets the colormap for the current figure to the specified predefined colormap.
🌐
MathWorks
mathworks.com › matlabcentral › answers › 185035-discrete-color-scale-in-3d-plot
Discrete color scale in 3D plot - MATLAB Answers - MATLAB Central
March 25, 2015 - How can I adjust the color bar so that I can use only one color to represent the data range [0 2), one color for (2 4], one color for (4 6], and so on? So the color on the strip will be discrete on each level, rather than a continuous transition. ... Sign in to comment. Sign in to answer this question. ... https://www.mathworks.com/matlabcentral/answers/185035-discrete-color-scale-in-3d-plot#answer_172624
🌐
MathWorks
mathworks.com › matlabcentral › answers › 2033754-how-to-change-the-scale-of-color-bar-to-discrete
How to change the scale of color bar to discrete ? - MATLAB Answers - MATLAB Central
October 15, 2023 - 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). ... https://www.mathworks.com/matlabcentral/answers/2033754-how-to-change-the-scale-of-color-bar-to-discrete#comment_2929291
🌐
MathWorks
mathworks.com › matlabcentral › answers › 428584-show-a-discrete-color-bar-in-a-contourf-plot-with-vector-defined-contours
Show a discrete color bar in a contourf plot with vector defined contours - MATLAB Answers - MATLAB Central
November 7, 2018 - My strategy was to plot with "full" jet colormap, and the constrain it to the number N of colors used (jet(N)) and adjusting the limits of the color bar. Again, this strategy is not working for every case. Sign in to comment. Sign in to answer this question. Share · Follow Question · Cris LaPierre on 17 Dec 2018 · Open in MATLAB Online · 0 votes · Share · Translate · Share a link to this answer · https://www.mathworks.com/matlabcentral/answers/428584-show-a-discrete-color-bar-in-a-contourf-plot-with-vector-defined-contours#answer_352708 ·
🌐
MathWorks
mathworks.com › matlab › graphics › labels and styling
Color and Styling - MATLAB & Simulink
Specify themes for figures, customize colors and line styles for 2-D plots, and select colormaps for surfaces, patches, and indexed images.
🌐
MathWorks
mathworks.com › matlabcentral › answers › 1905756-how-to-discretize-the-color-in-contour-bar-at-smallest-possible-range
How to discretize the color in contour bar at smallest possible range? - MATLAB Answers - MATLAB Central
February 3, 2023 - set(gca, 'Colormap',cmap) cb = colorbar(); caxis([0,3]) set(cb, 'Ticks', 0:0.25:3) ylabel(cb, 'Att rate') Sign in to comment. Sign in to answer this question. KSSV on 3 Feb 2023 · Link · × · https://www.mathworks.com/matlabcentral/answe...
🌐
MathWorks
mathworks.com › matlabcentral › answers › 276282-are-there-any-predefined-discrete-color-set-with-optimal-neighboring-contrasts
Are there any predefined discrete color set with optimal neighboring contrasts? - MATLAB Answers - MATLAB Central
March 30, 2016 - https://www.mathworks.com/matlabcentral/answers/276282-are-there-any-predefined-discrete-color-set-with-optimal-neighboring-contrasts#answer_215729 · Cancel Copy to Clipboard · You can try the lines() function to get a contrasting colormap. Sign in to comment.
🌐
MathWorks
mathworks.com › matlabcentral › answers › 263392-plot-raster-map-with-custom-colormap
Plot raster map with custom colormap - MATLAB Answers - MATLAB Central
January 11, 2016 - I have a matrix map_data associated with referencing object R (both in attached MAT-file). I want to map it with a discrete colorbar given an irregular range of values to look something like this: How can I do this using geoshow or something else? I'm using MATLAB r2014b. Here is the relevant information for the colormap:
🌐
MathWorks
mathworks.com › matlabcentral › fileexchange › 28073-use-of-discrete-colors-in-contourf-and-pcolor-with-colorbar
Use of discrete colors in contourf and pcolor with colorbar - File Exchange - MATLAB Central
October 18, 2010 - The number of colors in the colormap must be one plus the length of levels. While making the contourf plot make it on [min(data) Levels] levels. In addition to that keep max(Levels) <max(data) After plotting pcolor set the caxis on bounds [min(data) max(Levels)] All comments are welcome. Thanks Mak · Makarand (2026). Use of discrete colors in contourf and pcolor with colorbar (https://www.mathworks.com/matlabcentral/fileexchange/28073-use-of-discrete-colors-in-contourf-and-pcolor-with-colorbar), MATLAB Central File Exchange.
Top answer
1 of 2
1

Will had a great idea to use histc(), but I had to edit his code to make it work for me. Here's what I ended up with.

my_colormap = [204  204  204
               153  153  153
               255  255  178
               254  204   92
               253  141   60
               240   59   32
               189    0   38
                 0    0    0]/255 ;
binEdges = [0 0.001 0.005 0.01 0.05 0.1 0.25 0.5 1] ;
labels = textscan(num2str(binEdges*100),'%s') ;
labels = labels{1} ;
labels{length(labels)} = [labels{length(labels)} '%'] ;

[~,indices] = histc(map_data,binEdges);
indices(isnan(map_data)) = NaN ;
indices(isinf(map_data)) = NaN ;

figure ;
pcolor(indices-1)   % Instead of image(), to display NaN values as white
shading flat
axis equal tight

colormap(gca,my_colormap);   % gca as first argument prevents 
                             % colormap from changing for all 
                             % subsequent plots
h = colorbar;
caxis([0 length(binEdges)-1])
h.YTickLabel = labels ;

2 of 2
1

MATLAB only has built-in support for linear colormaps. Therefore for nonlinear mappings like this one, you need to transform the values of map_data so that the changes in color are evenly spaced. For a discrete colormap like this one, integer indices are ideal, and you can get them easily using histc:

ranges = [0 0.001 0.005 0.01 0.05 0.1 0.25 0.5 1];
[~,ind] = histc(map_data,ranges);

Use the indices in ind in lieu of the values in map_data as your color data, and then all you need is to apply the specified colors as a colormap. Where you need to label the true map_data values on your colorbar, manually relabel the YTickLabel of the corresponding colorbar.

I don't have the mapping toolbox to demonstrate this with geoshow, but displaying as a straightforward image works as follows:

image(ind)
axis equal tight
set(gca,'YDir','normal')
colormap([204  204  204
          153  153  153
          255  255  178
          254  204   92
          253  141   60
          240   59   32
          189    0   38
          0    0    0]/255);
h = colorbar;
h.YTickLabel = edges(h.YTick)*100;

Which results in the following: