使用patch命令的两个不同的colormaps - Matlab

问题描述 投票:1回答:1

我如何为以下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

这个想法是每个补丁都有不同的颜色图(也有颜色条)

enter image description here

matlab matlab-figure
1个回答
1
投票

您可以通过将多个颜色映射堆叠在一起来使用它们:

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

然后你可以为每个情节设置CDATA属性:

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

在你的情况下,你只需要缩放你的CDATA所以第一个情节的CDATA[1, cmapsize]和其他[cmapsize+1, cmapsize*2]的范围内:

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

完整代码:

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

据我所知,每个轴只能有一个颜色条。但是,您可以在颜色条的中间插入一个垫片(白色区域)来分隔两种颜色:

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

并调整刻度和标签:

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.

您还需要更改第二个绘图的CDATA以避免使用间隔区域:

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

这是您使用此方法可以获得的:

enter image description here

如果这对你来说不够好,你可以考虑按照@Hoki的评论(Multiple colormaps in one axis)的建议将一个轴叠加在另一个轴之上。

© www.soinside.com 2019 - 2024. All rights reserved.