Filled contour plot problem: colors and contour lines mismatch

18 views (last 30 days)
The aim is to create a filled contour plot with a matching colorbar. I have done this very often, but now I have a matrix of ocean salinity which is not plotted correctly. I have reduced the skript to the following lines, after loading the matrix "isalt", which contains Nans at the borders and values in the range of "csal" in between.
csal=[34.76:0.02:34.98];
figure(1)
[C,h]=contourf([1:72],[1:20],isalt,csal);
colormap(jet(length(csal)-1));
caxis([csal(1) csal(end)]);
colorbar;
The plot looks fine until "caxis" is carried out.
I am new in this forum and cannot find a possibility to upload the .mat file containing the matrix and the resulting figure :-/
But maybe someone has an idea...
  3 Comments
VMueller
VMueller on 21 Feb 2017
Edited: VMueller on 21 Feb 2017
Hi Kerstin,
I know it's been a long time since you asked the question and I assume your problem is solved by now. But I still wanted to reply for anyone who comes across similar problems.
To your second question: The white holes happen if values are out of range of the defined colors. I know this is weird, I would assume to just use the last defined color. But if it is out of range, it will be white.
The first question is a bit trickier. I found a solution (and again, please don't ask me why the contourf behavior is so weird, maybe someone else knows that).
What I did with my data (also oceanographic btw) is the following (I am using your example):
csal=[34.74:0.02:34.98]; %here I added one more level as a dummy
figure(1)
[C,h]=contourf([1:72],[1:20],isalt,csal);
colormap(jet(length(csal))); % one color more
caxis([csal(1) csal(end)]);
hbar=colorbar; %add a handle so you can adjust the colorbar
set(hbar,'Ylim',[csal(2) csal(end)]); %adjust the length of the colorbar
Maybe you also have to adjust the ticks of the colorbar. If you dont want to lose one color of your colormap, you might also manually add a line of zeros to the colormap (since they are not actually going to be used)
cmap=jet(length(csal)-1);
cmap =[cmap;0 0 0]; % or maybe at the top, I am not sure which colors are used first
colormap(cmap)
This workaround did the job for me.
Fanny W.
Fanny W. on 22 Feb 2017
Edited: Fanny W. on 22 Feb 2017
Hello Kristin,
the problem you encountered is a binning issue of colormaps. In order to use contourf/colorbar matlab transforms your colormap into one that is 64 color levels long (or some other length, but a specific one and probably a power of 2...) resulting in problematic determinations on which level the specific colors must start exactly... Contourf then uses this colormap and looks for the colors that must be used at exactly its given levels (Note here: contourf always starts with the low limit and fills up to the high limit, with the low limit determining the coloring).
csal=[34.74:0.02:34.98]; %here I added one more level as a dummy
Your interval of 0.24 psu or 12 colors respectively is not nicely divisible in 64 color levels, so you will get messed up boundaries between the colors you chose. Since 12*5=60 it ends up with 4 color levels being wider than the other 8 and therefore somewhat dominating the contourf-plot.
In order to prevent this problem you must define a colormap of 64 rows and make sure that the colors start at the right level and not slightly but essentially shifted.
Hope that helps...

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!