Assigning black color to zero values of "imagesc" plot
27 次查看(过去 30 天)
显示 更早的评论
I want to use "imagesc" to plot the following matrix:
A=[9 24 6 12 6;
0 33 24 0 12;
0 0 13 14 12;
0 0 0 0 0];
I want to assign black color to 0 values and set the color bar from 6 to 33.
0 个评论
采纳的回答
Image Analyst
2013-9-24
Try this:
A=[9 24 6 12 6;
0 33 24 0 12;
0 0 13 14 12;
0 0 0 0 0];
imagesc(A);
cmap = jet(max(A(:)));
% Make values 0-5 black:
cmap(1:6,:) = zeros(6,3);
colormap(cmap);
colorbar
2 个评论
Image Analyst
2013-9-25
A=[9 9.1 8.3 0 0;
0 9.5 8.4 0 0;
0 0 9.6 8.3 8.1;
0 0 0 9.7 0];
lowestValue = min(A(A(:)>0))
highestValue = max(A(:))
imagesc(A);
cmap = jet(256);
colormap(cmap);
caxis(gca,[lowestValue-2/256, highestValue]);
% Make less than lowest value black:
cmap(1,:)=[0,0,0];
colormap(cmap)
colorbar
更多回答(1 个)
Kelly Kearney
2013-9-24
It'll be a little tricky to do with imagesc, but you could get a similar result with pcolor:
aplt = nan(size(A)+1);
aplt(1:end-1,1:end-1) = A;
aplt(aplt == 0) = NaN;
pcolor(aplt);
colorbar;
set(gca, 'color', 'k', 'clim', [6 33], 'ydir', 'reverse')
2 个评论
Kelly Kearney
2013-9-25
Neither function does any smoothing, unless you use interpolated shading with the pcolor plot. If you're referring to the black outlines of each grid cell that is the default under pcolor, you can alter that with a simple
shading flat;
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Colormaps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!