Filter löschen
Filter löschen

How to use bluewhitered function?

11 Ansichten (letzte 30 Tage)
Yandong Lang
Yandong Lang am 7 Mär. 2019
Kommentiert: Yogesh Kumkar am 5 Mär. 2022
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?

Akzeptierte Antwort

Geoff Hayes
Geoff Hayes am 7 Mär. 2019
Bearbeitet: Geoff Hayes am 7 Mär. 2019
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!

Weitere Antworten (1)

Yogesh Kumkar
Yogesh Kumkar am 5 Mär. 2022
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');
  2 Kommentare
DGM
DGM am 5 Mär. 2022
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
Yogesh Kumkar am 5 Mär. 2022
set(gcf,'inverthardcopy',false)
works like a charm! Thanks.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by