Colorbar doesn't match with contourf plot

44 views (last 30 days)
Rakuen
Rakuen on 16 Jan 2021
Commented: Star Strider on 14 Aug 2022
As you see colors do not match with values. Is there anything I missed here?
figure
contourf(diff)
colormap('jet');
set(gca,'visible','off')
colorbar
caxis([0 100])
  4 Comments
Rakuen
Rakuen on 18 Jan 2021
It is an error plot and it tells a lot but it shouldn't matter.

Sign in to comment.

Answers (2)

Robert Lobbia
Robert Lobbia on 18 Jul 2022
I had this issue as well and discovered that I needed to include number of levels directly in the countourf call:
WORKS: contourf(x,y,ZZ,256)
DOES NOT WORK: contourf(x,y,ZZ)
the number of levels (256 above) needs to be close to dynamic range of data as CData mapping/binning occurs when contourf is called (can't change it later through any caxis/clim/colormap/colorbar settings). I only had this issue with datasets where the values of my matrix where orders of magnitude apart, which makes sense since the CData map matlab linearly created from the data just included the highest level as one color and most other values as the other color limit.
  2 Comments
Star Strider
Star Strider on 14 Aug 2022
Actually, it is not necessary to interpolate it, and the interpolation used in the posted code is incorrect. See my Answer to your previous post Contour Plot Wrong Colors.

Sign in to comment.


Cris LaPierre
Cris LaPierre on 18 Jan 2021
Edited: Cris LaPierre on 18 Jan 2021
I believe the issue is that this data set doesn't lend itself well to a contour plot. 98.6% of the data is <10 or NaN. Your lowest contour is set to the min value but that is a single point in the 256x256 array. The default contour levels are in steps of 5000 for your data (range of 0.00006-3.5e4). Everything below the min value is set to white, and everything between min and 5000 should be set to the min color. However there are not levels to speak of. Just individual points.
If you want to recreate the issue, see this simplified example.
figure
% most values are near zero. No structure, so no clear contour boundaries
Z=rand(25);
% Create one point that is much larger
Z(5,5)=35000;
contourf(Z);
colorbar
colormap('jet');
caxis([0 100])
I can't explain how it's picking the color it picks (it appears to be the color in the middle of the colormap), but the issue is related to your levels. Try setting the levels you want as an input using this syntax:
S=load('matlab.mat');
figure
contourf(S.diff,[0:10:100]);
colormap('jet');
set(gca,'visible','off')
colorbar
caxis([0 100])
  7 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!