Colorbar doesn't match with contourf plot

71 views (last 30 days)
Rakuen
Rakuen on 16 Jan 2021
Commented: Naseef Muhammed on 1 Nov 2023
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
Adam Danz
Adam Danz on 18 Jan 2021
I'm not sure what you're expecting to see from a contour of these data. As you can see in the surface plot below, the data are mostly flat.
load('matlab.mat')
surf(diff)
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
Alberto Mateos Bond
Alberto Mateos Bond on 14 Aug 2022
Hi!
Something similar happened to me.
I have made a contourf interpolating results with meshgrid and it plots a graph which does not correspond to the values that I put as input with the colors of the colorbar (There are points where it should be yellow or red and it is blue, or other points which have markedly different values and are the same color). I don't know if it's a problem with the interpolation method, I think not since I've checked that and the numbers are consistent. I don't see a problem with the contour either. I don't know if I should use another interpolation method and I dont unterstand what exactly is wrong
clear all;clc;
A=[77.3139227401096,77.3120257063849,77.3120257063849,77.3120257063849,77.3120257063849,77.3120257063849,77.3120257063849,77.3120257063849,77.3120257063849,77.8499350764564,77.8442382759329,77.8400158037560,77.8367460305253,77.8341382601430,77.3189088790396,77.3120257063849,77.3120257063849,77.3120257063849,77.3120257063849,77.4228460196533,77.3770975908979,77.3678714954335,77.3624256179988,77.3588314539844,77.3562741687948,77.3543880471537,77.3529118848689,77.3517359358068,77.8230143346564,77.5947167588728,77.4572289300964,77.3536200322737,77.2815354478640,77.1958950775090,77.1462000726089,77.1176650859710,77.1045877194747,77.0939198939992,77.0960045406636,77.0936701640036,77.0918470331206,77.0903910018129,78.9250781251261,78.2607772080247,77.8571819100295,77.5445345646900,77.1777675777582,77.1067936547286,77.0560757369439,77.0204796252278,77.0021470133600,76.9934357792392,76.9874018876342,76.9841903129164,76.9834146935698,76.9832804850934];
torque=[-40,-30,-20,-10,10,20,30,40,50,60,70,80,90,100,-40,-30,-20,-10,10,20,30,40,50,60,70,80,90,100,-40,-30,-20,-10,10,20,30,40,50,60,70,80,90,100,-40,-30,-20,-10,10,20,30,40,50,60,70,80,90,100];
rpmrange=[6,6,6,6,6,6,6,6,6,6,6,6,6,6,8,8,8,8,8,8,8,8,8,8,8,8,8,8,10,10,10,10,10,10,10,10,10,10,10,10,10,10,12.5,12.5,12.5,12.5,12.5,12.5,12.5,12.5,12.5,12.5,12.5,12.5,12.5,12.5];
[xmesh,ymesh]=meshgrid(torque,rpmrange);
vq=griddata(torque,rpmrange,A,xmesh,ymesh);
contourf(xmesh,ymesh,vq,1000,'EdgeColor','none')
xlabel('Torque increase [%]');
ylabel('RPM range [*10^3]');
title('Torque Sensivity Study');
hc = colorbar;
set(get(hc,'title'),'string','LapTime [s]')
colormap(flipud(jet))
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])
  8 Comments
Naseef Muhammed
Naseef Muhammed on 1 Nov 2023
setting "colormap('jet100')" will create the figure like what you want. That is, Number of levels, caxis and colormap value (here, jet100) must be same!

Sign in to comment.

Categories

Find more on Contour Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!