更改Colorbar计划

时间:2015-11-30 20:23:02

标签: matlab matlab-figure colorbar

我在MATLAB中读取图像并使用imagesc显示它。然后,我将colormap设置为grey

在此图像的顶部,我使用喷射颜色方案绘制点。然后,如何显示jet colorbar,以对应于原始图像顶部绘制的点的颜色?我尝试在所有绘图之后重新定义colorbar,但这会将原始灰度图像更改回颜色,这是不合适的。

代码:

%Create Figure with handle.
h5=figure('units','normalized','outerposition',[0 0 1 1]);
whitebg(h5,[0 0 0]);
subplot(2,5,1);
k=1;
for i=16:25
    subplot(2,5,k);
    imagesc(squeeze(ana(:,:,i)));
    title(['Z=',num2str(i)]);
    colormap gray
    axis equal
    k=k+1;
    colorbar
end

%Adapt colour values so that they are between 0 and 1. We want to scale
%both data sets equally, so we find the smallest value across Ix and Iy. We
%also find what will be the new largest value across Ix and Iy, after we
%add the magnitude of the smallest value to make all numbers greater than
%or equal to 0.
absolutemin=min(min(Ix(:,1)),min(Iy(:,1)));
absolutemax=max(abs(absolutemin)+(max(Ix(:,1))),abs(absolutemin)+max(Iy(:,1)));

%Add the smallest value, and divide by the largest maximum value for both Ix
%and Iy.
ixcolours=uint8(((Ix(:,1)+abs(absolutemin))/absolutemax).*255)+1;
iycolours=uint8(((Iy(:,1)+abs(absolutemin))/absolutemax).*255)+1;

mycolours=jet(256);
o=1;
for k=16:25; %For all 3D slices

    for i=1:471; %and for all x and y seed slices
        if k==seed_locs(i,3);
            subplot(2,5,o);
            hold all%go to the corresponding z subplot
            plot(seed_locs(i,1),seed_locs(i,2),'MarkerFaceColor',mycolours(ixcolours(i),:),'MarkerEdgeColor',mycolours(ixcolours(i),:),'MarkerSize',10,'Marker','s') %plot the x and y seedlocs
            %hold on
        end
    end

    for i=1:486;
        if k==test_locs(i,3);
            subplot(2,5,o);
            hold all
            plot(test_locs(i,1),test_locs(i,2),'MarkerFaceColor',mycolours(iycolours(i),:),'MarkerEdgeColor',mycolours(iycolours(i),:),'MarkerSize',10,'Marker','s') %plot the x and y seedlocs
%             hold on
        end

    end
    o=o+1; %go to the next z subplot
end
colormap jet
colorbar

1 个答案:

答案 0 :(得分:3)

我认为以下示例可以帮助您改进代码。首先需要定义两个颜色图:

colormap([cool(64);gray(64)]);

然后假设我们有两套不同的数据点来绘制:

[X,Y,Z] = peaks(25);
h(1) = surf(X,Y,Z);hold on
h(2) = pcolor(X,Y,Z);

所以数据是用两个不同的句柄定义的。现在我们需要使用最小值和最大值来制作CData。

cmin = min(Z(:));
cmax = max(Z(:));

C1 = min(64,round((64-1)*(Z-cmin)/(cmax-cmin))+1);   % CData for the first datapoints

C2 = 64+C1;    % CData for the second datapoints

现在我们更新每个对象的CDatas。

set(h(1),'CData',C1);
set(h(2),'CData',C2);

现在您可以设置轴的CLim属性:

caxis([min(C1(:)) max(C2(:))])
colorbar;

enter image description here