How to avoid white patch in contourf ?

I am facing a problem from quite a long time. My data range is beyond my colorbar limit. That is why when the minimum value exceeded the colorbar limit it is coming white but it is okay for maximum value. How to solve this? In python something like extend = both works but what about MATLAB?
PS: I don't want to extend my colorbar limit.

8 comentarios

Image Analyst
Image Analyst el 23 de Mzo. de 2018
Attach your data, code, and a screenshot.
DIPANJAN DEY
DIPANJAN DEY el 23 de Mzo. de 2018
Editada: DIPANJAN DEY el 23 de Mzo. de 2018
Data set is too big to upload. you can see in the image that there are values greater than 1.9 but it appears dark red ( which is ok) but when values are less than -1.9, it should be dark blue but it came as white.
PS: I checked contourf.m and there in the comment they told like my minimum colorbar limit should be the minimum value of the dataset, otherwise they will treat that as an exception and left without filling it. But I don't want to extend my colorbar limit.
Walter Roberson
Walter Roberson el 23 de Mzo. de 2018
contourf( max(YourData, -1.9), ...)
DIPANJAN DEY
DIPANJAN DEY el 23 de Mzo. de 2018
No. It will not do my work. If i try to define the data values lower than -1.9 = -1.9, then the shape of my desired result will be different which is not acceptable. Anyway thanks for your response.
Walter Roberson
Walter Roberson el 23 de Mzo. de 2018
You want the values < -1.9 to come out the same color as the -1.9 values, so how could the shape be different?
Now if you had said that you need the data cursor to show the original values, then that would be different.
DIPANJAN DEY
DIPANJAN DEY el 23 de Mzo. de 2018
Editada: DIPANJAN DEY el 23 de Mzo. de 2018
Can you please tell me why in the upper limit it is not happening? Also yes I want to see the original values as I can see now for the dark red regions (above 1.9).
Image Analyst
Image Analyst el 23 de Mzo. de 2018
You say "when the minimum value exceeded the colorbar limit it is coming white" however, the minimum value (according to the colors) seems to be -1.9. However not all values that exceed your minimum value are white. I see values showing up as red and blue also. In fact, it seems like the only values showing up as white are around the range -0.1 to +0.1.
Anyway, let's assume you want that range to be some color other than white. You can just set the middle of the colormap to that color. What color do you want it to be?
DIPANJAN DEY
DIPANJAN DEY el 23 de Mzo. de 2018
The middle portion of my colorbar is white due to my colormap. But the white portion in the middle of the figure is not due to the colormap and the values there are lower than -1.9. If you use a different colormap then also you can see this white region.

Iniciar sesión para comentar.

Respuestas (2)

Kelly Kearney
Kelly Kearney el 23 de Mzo. de 2018

2 votos

Matlab's contourf function doesn't actually plot contour faces for lower-than-lowest-clevel regions when are unenclosed. (And for that matter, what it does with enclosed lower-than-lowest-clevel regions varies between Matlab versions).
I think my contourfcmap function with the calccontour option should be able to create the plot the way you'd like it.
Alternatively, you can set your axis color to match the lowest value in your colormap... that way empty contour regions will appear the correct color.

4 comentarios

DIPANJAN DEY
DIPANJAN DEY el 23 de Mzo. de 2018
Editada: DIPANJAN DEY el 24 de Mzo. de 2018
I tried your contourfcmap function also but it also shows the same plot. I am using MATLAB2016b version.
Kelly Kearney
Kelly Kearney el 23 de Mzo. de 2018
Editada: Kelly Kearney el 23 de Mzo. de 2018
The NaNs wreak a bit of havoc in my function (I've tried to come up with ways to deal with them... but things it's not always successful). In your case, things work smoothly if you remove the NaNs first (which doesn't really affect the contours, given that your NaNs just bracket the data):
Tmp = load('contourf_prob.mat');
[x,y] = meshgrid(Tmp.lat_a, Tmp.lev);
isn = all(isnan(Tmp.psi),1);
ax(1) = subplot(2,1,1);
contourf(Tmp.lat_a(~isn), Tmp.lev, Tmp.psi(:,~isn), Tmp.zlevs);
set(ax(1), 'clim', Tmp.zlevs([1 end]));
hold on;
scatter(x(:), y(:), 10, Tmp.psi(:), 'filled');
colormap(ccode(19));
colorbar('eastoutside');
title('Matlab''s contourf');
ax(2) = subplot(2,1,2);
contourfcmap(Tmp.lat_a(~isn), Tmp.lev, Tmp.psi(:,~isn), Tmp.zlevs, ...
ccode(19),'lo',[0 0 0.540],'hi',[0.540 0 0], ...
'cbarloc','eastoutside','method','calccontour');
% contourf(Tmp.lat_a, Tmp.lev, Tmp.psi, Tmp.zlevs);
title('My contourfcmap');
hold on;
hs = scatter(x(:), y(:), 10, Tmp.psi(:), 'filled');
set(gca, 'clim', minmax(Tmp.zlevs));
colormap(ccode(19));
set(ax, 'ydir', 'reverse');
(Edited to add scatter plot that demonstrates that yes, contourf is misrepresenting the below-lowest region).
DIPANJAN DEY
DIPANJAN DEY el 23 de Mzo. de 2018
Editada: DIPANJAN DEY el 23 de Mzo. de 2018
Thanks for your help. Yes this is the plot I want it but now the x_axis reduces as we omitted the nans, also if we increase the x-axis there is straight line coming at the end. Which is in my case not acceptable. Another thing, now i am unable to move the colorbar to my desired location (other than specified). Data cursor also not showing the value, i cannot specify my line style
Kelly Kearney
Kelly Kearney el 26 de Mzo. de 2018
I'm working on a fix to the NaN issue... may take a day or two. The colorbar enhancement is one that's been on my to-do list for a while... I'll see if I have time for that one as well, but no promises.
You can specify the line style by saving the handles structure (first output of contourfcmap) and then applying any changes you'd like.
Not sure about the data cursor thing... I don't use that myself. If it relies on interacting with a contourgroup object, then that's not really something I can offer, since my function uses simple patches and lines.

Iniciar sesión para comentar.

ilya
ilya el 26 de Oct. de 2025
You're probably not still looking for a fix for this, but for anyone having the same issue in 2025:
This is because if you set limits on contourf, it will not assign a color to any values below the minimum. This is dumb, so to fix it, let's say you have:
[x, y] = meshgrid(-1:0.1:1, -1:0.1:1); % x-y plane from -1 to 1
lvls = linspace(0.3, 1, 10); % contours from 0.3 to 1\
data = x.^2 + y.^2; % data varies from 0 to 2 (plug in x,y to test)
contourf(x, y, data, lvls)
You'll notice areas above 1 are shaded but areas below 0.3 are not. If you add an extra sublevel to lvls that corresponds to the minimum of your data and then use clim to tighten your axes back to the original bounds, you'll see the area below 0.3 is now shaded:
[x, y] = meshgrid(-1:0.1:1, -1:0.1:1);
lvls = linspace(0.3, 1, 10);
data = x.^2 + y.^2;
lvls = [min(data(:)), lvls]; % add the min of data to the beginning of lvls
contourf(x, y, data, lvls)
clim([lvls(2) lvls(end)]) % reset colorbar axes to original bounds
No need to download any extra functions or anything.

Categorías

Más información sobre Color and Styling en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 23 de Mzo. de 2018

Respondida:

el 26 de Oct. de 2025

Community Treasure Hunt

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

Start Hunting!

Translated by