How do I change the color distribution in contourf?

7 views (last 30 days)
Dhruv Thakkar
Dhruv Thakkar on 18 Jan 2022
Edited: DGM on 18 Jan 2022
I am trying to make a filled contour as in the following MWE.
%% Data aa is on the z - axis
aa = [190 201 196 187 202 205 192 200 215 207 201 222 224 206 229 248 235 291 321 317 281 191 202 197 188 203 206 194 202 216 208 203 224 227 208 235 255 241 4898 4898 4898 4898];
[x,y] = meshgrid([-10 -6 -2 2 6 10],[15 20 25 30 35 40 45]); %Meshgrid for x & y for the contour
%% Create the meshgrid for z for plotting
z = size(x);
z = zeros(size(x));
for i=1:6
z(:,i) = aa(7*(i-1)+1:7*i); % 7 because thats how my data is distributed in the array "aa"
end
%% Create the contour
contourf(y,x,z,30,"LineStyle","none")
h = colorbar;
But due to the large values in the array "aa" the whole plot is getting poloarized towards it.
Is there a way in which I can make the contour plot of the rest values without having the color gradient being affected by the large values and then denote the large values with red spot(red showing higher value in the contour)?
  2 Comments
Dhruv Thakkar
Dhruv Thakkar on 18 Jan 2022
Hey @KSSV,
Thank you for the suggestion. Maybe I didn't frame my question correctly. if I use caxis I am still losing the information and variation in the lower data range.

Sign in to comment.

Answers (1)

DGM
DGM on 18 Jan 2022
Edited: DGM on 18 Jan 2022
This probably isn't exactly what you want, but I think that an inpainting+marker approach would be visually misleading. In this example, I simply omit the extreme values by replacing them with NaN. The result is that no interpolation is performed between said points and their neighbors.
% Data aa is on the z - axis
aa = [190 201 196 187 202 205 192 200 215 207 201 222 224 206 229 248 235 291 321 317 281 191 202 197 188 203 206 194 202 216 208 203 224 227 208 235 255 241 4898 4898 4898 4898];
[x,y] = meshgrid([-10 -6 -2 2 6 10],[15 20 25 30 35 40 45]); %Meshgrid for x & y for the contour
% Create the meshgrid for z for plotting
z = reshape(aa,size(x));
% omit extreme values
z(z>500) = NaN;
% Create the contour
contourf(y,x,z,30,"LineStyle","none")
h = colorbar;
Note the use of reshape().
Alternatively, you could just constrain everything to a fixed z interval. Note that you have to constrain both caxis() and the levels used by contourf().
% Data aa is on the z - axis
aa = [190 201 196 187 202 205 192 200 215 207 201 222 224 206 229 248 235 291 321 317 281 191 202 197 188 203 206 194 202 216 208 203 224 227 208 235 255 241 4898 4898 4898 4898];
[x,y] = meshgrid([-10 -6 -2 2 6 10],[15 20 25 30 35 40 45]); %Meshgrid for x & y for the contour
% Create the meshgrid for z for plotting
z = reshape(aa,size(x));
% Create the contour
clim = [min(z(:)) 320]; % pick some upper limit
v = linspace(clim(1),clim(2),30);
contourf(y,x,z,v,"LineStyle","none")
h = colorbar;
caxis(clim) % pick some upper limit

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!