contourf plot value not corresponding to the legend bar value
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have a general question about a contourf() plot that I am plotting. The plot is shown in the screenshot below. What I am confused about is why different z-value (Level, in this case) have the same color. The plot only have two colors, blue and red, while there are many different values ranging from .22 to .26. For example, with Level value of .26, I am expecting it to be black, according to the bar on the right hand side, and .255 to be darker blue, and .2575 to be black as well, but it's only one shade of blue. It looks like anything above .25 is blue, and anything below is red. Can anyone shine some light on what is happening?
Here is my code.
close('all'), clear, clc
f1 = figure;
ax = axes(f1);
load('data.mat')
smplX = reshape(smplX,[],1);
smplY = reshape(smplY,[],1);
smplZ = reshape(smplZ,[],1);
xLimits = [round(min(smplX)-1) round(max(smplX)+1)];
yLimits = [round(min(smplY)-1) round(max(smplY)+1)];
[xq,yq] = meshgrid(xLimits(1):0.5:xLimits(2),yLimits(1):0.5:yLimits(2));
zq2 = griddata(smplX,smplY,smplZ,xq,yq,'linear');
if any(isnan(zq2))
% zq3 = griddata(smplX,smplY,smplZ,xq,yq,'nearest');
% idx = isnan(zq2);
% zq2(idx)=zq3(idx);
zq2 = griddata(smplX,smplY,smplZ,xq,yq,'nearest');
end
contourf(ax,xq,yq,zq2);
c = colormap(ax,flipud(turbo()));
ax.CLim = [0.22 max(zq2,[],'all')];
cInc = (ax.CLim(2)-ax.CLim(1))/256;
cInc = round((0.25 - ax.CLim(1))/cInc);
if cInc>size(c,1)
cInc = size(c,1);
disp(size(c,1));
end
% rectangle(ax,'Position',[-xLimits(1) yLimits(1) ...
% xLimits(2)-xLimits(1) ...
% yLimits(2)-yLimits(1)],...
% 'FaceColor',...
% c(cInc,:),'LineWidth',2)
ax.XLimitMethod = "tight";
ax.YLimitMethod = "tight";
ax.Children = ax.Children(end:-1:1);
colorbar(ax,"eastoutside")
0 commentaires
Réponse acceptée
Voss
le 14 Déc 2023
"It looks like anything above .25 is blue, and anything below is red."
This is happening because of the levels that contourf chose. Take a look at the levels by taking the second output from contourf, the contour object, and inspecting its 'LevelList' property:
f1 = figure;
ax = axes(f1);
load('data.mat')
smplX = reshape(smplX,[],1);
smplY = reshape(smplY,[],1);
smplZ = reshape(smplZ,[],1);
xLimits = [round(min(smplX)-1) round(max(smplX)+1)];
yLimits = [round(min(smplY)-1) round(max(smplY)+1)];
[xq,yq] = meshgrid(xLimits(1):0.5:xLimits(2),yLimits(1):0.5:yLimits(2));
zq2 = griddata(smplX,smplY,smplZ,xq,yq,'linear');
if any(isnan(zq2))
% zq3 = griddata(smplX,smplY,smplZ,xq,yq,'nearest');
% idx = isnan(zq2);
% zq2(idx)=zq3(idx);
zq2 = griddata(smplX,smplY,smplZ,xq,yq,'nearest');
end
[~,c_obj] = contourf(ax,xq,yq,zq2);
c = colormap(ax,flipud(turbo()));
ax.CLim = [0.22 max(zq2,[],'all')];
cInc = (ax.CLim(2)-ax.CLim(1))/256;
cInc = round((0.25 - ax.CLim(1))/cInc);
if cInc>size(c,1)
cInc = size(c,1);
disp(size(c,1));
end
% rectangle(ax,'Position',[-xLimits(1) yLimits(1) ...
% xLimits(2)-xLimits(1) ...
% yLimits(2)-yLimits(1)],...
% 'FaceColor',...
% c(cInc,:),'LineWidth',2)
ax.XLimitMethod = "tight";
ax.YLimitMethod = "tight";
ax.Children = ax.Children(end:-1:1);
colorbar(ax,"eastoutside")
disp(c_obj.LevelList)
Those levels are not particularly suitable to your data. Only one of them (0.25) is within the range of data you care about. You can specify your own levels as input to contourf instead. For example:
f1 = figure;
ax = axes(f1);
load('data.mat')
smplX = reshape(smplX,[],1);
smplY = reshape(smplY,[],1);
smplZ = reshape(smplZ,[],1);
xLimits = [round(min(smplX)-1) round(max(smplX)+1)];
yLimits = [round(min(smplY)-1) round(max(smplY)+1)];
[xq,yq] = meshgrid(xLimits(1):0.5:xLimits(2),yLimits(1):0.5:yLimits(2));
zq2 = griddata(smplX,smplY,smplZ,xq,yq,'linear');
if any(isnan(zq2))
% zq3 = griddata(smplX,smplY,smplZ,xq,yq,'nearest');
% idx = isnan(zq2);
% zq2(idx)=zq3(idx);
zq2 = griddata(smplX,smplY,smplZ,xq,yq,'nearest');
end
[~,c_obj] = contourf(ax,xq,yq,zq2,'LevelList',[0 linspace(0.22,0.26,5)]);
c = colormap(ax,flipud(turbo()));
ax.CLim = [0.22 max(zq2,[],'all')];
cInc = (ax.CLim(2)-ax.CLim(1))/256;
cInc = round((0.25 - ax.CLim(1))/cInc);
if cInc>size(c,1)
cInc = size(c,1);
disp(size(c,1));
end
% rectangle(ax,'Position',[-xLimits(1) yLimits(1) ...
% xLimits(2)-xLimits(1) ...
% yLimits(2)-yLimits(1)],...
% 'FaceColor',...
% c(cInc,:),'LineWidth',2)
ax.XLimitMethod = "tight";
ax.YLimitMethod = "tight";
ax.Children = ax.Children(end:-1:1);
colorbar(ax,"eastoutside")
disp(c_obj.LevelList)
I'm not sure that makes for a clear contour plot, but now you've got plenty of color variation.
2 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Contour Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!