Custom colormap for a contourf plot?

26 views (last 30 days)
Victor Kolobov
Victor Kolobov on 26 Jul 2019
Answered: Ganesh Regoti on 2 Aug 2019
I am working on a contourf plot and my Z matrix contains heights between -2 and 2e+15 and the entries for which Z is undefined contain (-3).
1. I want a custom colormap for the Z filled contours such that:
The (-3) value contours will be colored in white.
The contours in the range (-2,0] will be colored in different shades of black linearly dependent on the heights.
The contours in the range [0,2) will be colored in different shades of magenta linearly dependent on the heights.
The contours in the range [2,10) will be colored in different shades of blue linearly dependent on the heights.
[10,1e+2) :different shades of cyan linearly dependent on the heights.
[1e+2,1e+4) :different shades of green linearly dependent on the heights.
[1e+4,1e+8) :different shades of yellow linearly dependent on the heights.
[1e+8,1e+16) :different shades of red linearly dependent on the heights.
.
2. I want a custom colorbar with all the ranges boundaries (and only them) mentioned on it and with different areas allocated to each range to illustrate their different magnitudes.
3. Only if possible: to allocate a small area to the white color at the bottom of this colorbar and instead of (-3), to write there: 'outside of the function domain'.
  1 Comment
dpb
dpb on 27 Jul 2019
On a linear scale with values from ~0 to 10^15, only the top of the axis values will be anything visually but a plane at the origin.

Sign in to comment.

Answers (1)

Ganesh Regoti
Ganesh Regoti on 2 Aug 2019
See the following code which may help you
%% Colorbar ranges
crange = [-3 -2; -2 0; 0 2; 2 10; 10 100;100 10^4;10^4 10^8; 10^8 10^16];
cmap = rand(size(crange,1),3); %Specify your customized colors in RGB format
%% Data
[X, Y] = meshgrid(linspace(1,2^15,1000),linspace(0,2^15,1000));
A = X + Y; % I have used random data set.
%% Arrange the colors range
colors = zeros(size(A));
for i = 1:size(crange,1) % Categorizing the values which fall into specified range
colors(A > crange(i,1) & A<=crange(i,2)) = crange(i,2);
end
contourf(X,Y,colors,8);
%% Color bar
h = colorbar;
h.Ticks = -3:((10^8 +3)/8):10^8; % Set ticks for all ranges.
h.TickLabels = {'outside of function domain','-2','0','2','10','100','10^4','10^8','10^16'}; % Set tick labels.
caxis([-3 10^8]); % Set colormap limits
colormap(cmap);

Categories

Find more on Colormaps in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!