Colorbar doesn't match with contourf plot
43 views (last 30 days)
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:
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.
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.
% most values are near zero. No structure, so no clear contour boundaries
% Create one point that is much larger
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: