How to use bluewhitered function?
9 次查看(过去 30 天)
显示 更早的评论
The bluewhitered function is a very used map plotting function. I want to know how to add limits, and how to set the center of the map. For example, if I want to plot a map with a range of [1,3], how to set the 2 to be the center and to be white?
采纳的回答
Geoff Hayes
2019-3-7
编辑:Geoff Hayes
2019-3-7
Yandong - the bluewhitered code seems to have been intended to work under the assumption that it returns an M-by-3 matrix containing a blue to white to red colormap, with white corresponding to the CAXIS value closest to zero. In your case, you want :"white corresponding to the CAXIS value closes to two". I think that you can do this if you just ensure that the code corresponding to the first if statement is evaluated. Obviously you won't have positive and negative limits, so you could just replace the if-elseif-else block with
ratio = 0.5; % abs(lims(1)) / (abs(lims(1)) + lims(2));
neglen = round(m*ratio);
poslen = m - neglen;
% Just negative
new = [bottom; botmiddle; middle];
len = length(new);
oldsteps = linspace(0, 1, len);
newsteps = linspace(0, 1, neglen);
newmap1 = zeros(neglen, 3);
for i=1:3
% Interpolate over RGB spaces of colormap
newmap1(:,i) = min(max(interp1(oldsteps, new(:,i), newsteps)', 0), 1);
end
% Just positive
new = [middle; topmiddle; top];
len = length(new);
oldsteps = linspace(0, 1, len);
newsteps = linspace(0, 1, poslen);
newmap = zeros(poslen, 3);
for i=1:3
% Interpolate over RGB spaces of colormap
newmap(:,i) = min(max(interp1(oldsteps, new(:,i), newsteps)', 0), 1);
end
% And put 'em together
newmap = [newmap1; newmap];
In the above, we set the ratio variable to be 0.5 since you want 2 to correspond to white and 2 is the middle value in the interval [1,3]. And that is it - there are no other code changes. Try it out and see what happens!
更多回答(1 个)
Yogesh Kumkar
2022-3-5
I have tested the function. The figure 1 is wrong because -10 is NaN (gray). The figure 2 is correct but when I save it, I get NaN as white. Please suggest how to fix it.
Z=randi(10,5);
Y=randi([-10 10],5);
Z([2,10,13,24])=nan;Y([3,12,23])=nan;
figure(2); % first method: wrong
ax11=subplot(1,2,1);imagesc(Z);caxis([-10 10]);
cmap11=colormap(ax11,bluewhitered(22));
cmap11(1,:) = [0.5 0.5 0.5];
colormap(ax11,cmap11); h11=(colorbar);
ax12=subplot(1,2,2);imagesc(Y);caxis([-10 10]);
cmap12=colormap(ax12,bluewhitered(22));
cmap12(1,:) = [0.5 0.5 0.5];
colormap(ax12,cmap12); h12=(colorbar);
sgtitle('Figure 1 (cmap(1,:)=[0.5 0.5 0.5])','fontweight','bold','fontsize',16);
saveas(gcf,'/Volumes/YKMac/PAPER_3/Figures_P3/zTestNaN1.png');
%$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
figure(3); % second method: correct
subplot(1,2,1);h3=imagesc(Z);caxis([-10 10]);
set(h3,'alphadata',~isnan(Z));
set(gca,'color', [0.5 0.5 0.5]);
colormap(bluewhitered(22)); colorbar; hold on;
subplot(1,2,2);h4=imagesc(Y);caxis([-10 10]);
set(h4,'alphadata',~isnan(Y));
set(gca,'color', [0.5 0.5 0.5]);
colormap(bluewhitered(22)); colorbar; hold on;
sgtitle('Figure 2 (set alphadata ~isnan(Y))','fontweight','bold','fontsize',16);
saveas(gcf,'/Volumes/YKMac/PAPER_3/Figures_P3/zTestNaN2.png');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/915214/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/915219/image.png)
2 个评论
DGM
2022-3-5
This is a common problem when trying to rely on the axes background color. Make sure to unset the 'inverthardcopy' property of the figure.
Z=randi(10,5);
Y=randi([-10 10],5);
Z([2,10,13,24])=nan;Y([3,12,23])=nan;
% second method: correct
subplot(1,2,1);h3=imagesc(Z);caxis([-10 10]);
set(h3,'alphadata',~isnan(Z));
set(gca,'color', [0.5 0.5 0.5]);
colormap(bluewhitered(22)); colorbar; hold on;
subplot(1,2,2);h4=imagesc(Y);caxis([-10 10]);
set(h4,'alphadata',~isnan(Y));
set(gca,'color', [0.5 0.5 0.5]);
colormap(bluewhitered(22)); colorbar; hold on;
sgtitle('Figure 2 (set alphadata ~isnan(Y))','fontweight','bold','fontsize',16);
% write the figure as a png
set(gcf,'inverthardcopy',false)
fname = 'zTestNaN2.png';
saveas(gcf,fname);
% read it back and display it
A = imread(fname);
clf
imshow(A)
Yogesh Kumkar
2022-3-5
set(gcf,'inverthardcopy',false)
works like a charm! Thanks.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Red 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!