jarhead
jarhead

Reputation: 1901

Two different colormaps using patch command - Matlab

How Can I produce two different color maps for the following patch

N=120;   
ids = (1:N/2)';
faces = [ids, ids+1, N-ids, N-ids+1];
c = exp(-6*cos(theta))';
c2 = exp(-6*cos(pi/2-theta))'; 
theta = linspace(0,2*pi,N+1); theta(end) = [];
figure
hold on
patch('Faces', faces, 'Vertices',[cos(theta);sin(theta)]','FaceVertexCData',c, 'FaceColor', 'interp', 'EdgeColor', 'none')
patch('Faces', 1:120, 'Vertices',1.01*[cos(theta);sin(theta)]','FaceVertexCData',c2, 'FaceColor', 'none', 'EdgeColor', 'interp','linewidth',5)
axis equal

The idea is that each patch will have a different colormap (with colorbars as well)

enter image description here

Upvotes: 1

Views: 400

Answers (1)

Anthony
Anthony

Reputation: 3793

You can use multiple colormaps in one axes by stacking them together:

cmapsize = 64;
colormap( [parula(cmapsize); jet(cmapsize)] );

Then you can set the CDATA property for each plot:

c1 = 1:cmapsize; %this uses the first colormap.
c2 = cmapsize+1 : cmapsize*2; % this uses the second colormap.

In your case, you just have to scale your CDATA so the CDATA for the first plot is within the range of [1, cmapsize] and the other [cmapsize+1, cmapsize*2]:

c = normalize(exp(-6*cos(theta)),'range')' * cmapsize;
c2 = normalize(exp(-6*cos(pi/2-theta)),'range')' * cmapsize + cmapsize + 1;

The full code:

N=120;   
cmapsize = 64;

ids = (1:N/2)';

theta = linspace(0,2*pi,N+1); theta(end) = [];
faces = [ids, ids+1, N-ids, N-ids+1];
c = normalize(exp(-6*cos(theta)),'range')' * cmapsize;
c2 = normalize(exp(-6*cos(pi/2-theta)),'range')' * cmapsize + cmapsize + 1;

figure('colormap', [parula(cmapsize);jet(cmapsize)]);
hold on
patch('Faces', faces, 'Vertices',[cos(theta);sin(theta)]','FaceVertexCData',c, 'FaceColor', 'interp', 'EdgeColor', 'none')
patch('Faces', 1:120, 'Vertices',1.01*[cos(theta);sin(theta)]','FaceVertexCData',c2, 'FaceColor', 'none', 'EdgeColor', 'interp','linewidth',5)
axis equal

enter image description here


Colorbar

As far as I know, each axes can only have one colorbar. However, you can insert a spacer (white-color area) in middle of the colorbar to separate the two colors:

spacer = 10;
figure('colormap', [parula(cmapsize); ones(spacer,3); jet(cmapsize)]);

And adjust the ticks and labels:

f = colorbar;
ticks = linspace(0,cmapsize,5);
f.Ticks = [ticks, ticks + cmapsize + spacer + 1];
f.TickLabels = compose('%d',ticks); % or whatever your tick labels are.

You also need to change your CDATA for the second plot to avoid using the spacer area:

c2 = normalize(exp(-6*cos(pi/2-theta)),'range')' * cmapsize + cmapsize + 1 + spacer;

This is what you can get with this method:

enter image description here

If this is not good enough for you, you may consider overlaying one axes on top of another as suggested in @Hoki's comment (Multiple colormaps in one axis).

Upvotes: 1

Related Questions