Contourf fill smaller instead of larger

13 views (last 30 days)
Hi Folks,
By default, contourf fills colour equal to or greater than the chosen level.
Is it possible to make it do the opposite (i.e. fill with colour all areas lower than the specified level)?
I know a work around would be to just plot the negative of the data and flip the colormap, but ideally I want to use it to highlight positive and negative regions on the same plot, which using that workaround would require 2 colormaps on 1 axes which is problematic.
For example, the following code takes the "peaks" surface, and highlights everything greater than +2 in yellow, everything between -2 and +2 as dark blue, and everything lower than -2 remains white....
figure;
contour(peaks,[-2 2])
What I'd like it to do is what I photoshopped in the following image, to highlight everything greater than +2 as yellow (as before) but everything less than -2 as dark blue, and leave everything between -2 and +2 as white. Like this...
Ideally something that can be done programatically, as I need to do multiple levels and for many hundreds of plots!
Thanks,
Daniel

Answers (1)

Bjorn Gustavsson
Bjorn Gustavsson on 23 Sep 2020
You "simply" have to create a colormap that is white (or suitable levels of pale, or black in case of that making more esthetic sense) in the middle, there are a couple of such colormap-tools on the file exchange: red-blue #1, custom colormaps, pycolormap4matlab, crameri colormaps, colorbrewer (I'm sure there's more). Some of these packages give you multiple colormaps, the red-blue is obviously a red-white-blue colormap, all will illustrate the concept so that you can start tinkering with something that makes the presentation of your data great.
HTH
  2 Comments
Daniel Tudball Smith
Daniel Tudball Smith on 5 Oct 2020
Edited: Daniel Tudball Smith on 5 Oct 2020
Thanks, that's actually what I am already doing (I just used the default in my example for simplicity sake), the problem is not with the colormap, it's with how matlab chooses to apply a colour to the contour bands from the colormap.
It's very evident if you only want to plot a few contour levels, that the band between two levels is coloured by the lower of the two, so the negative values tend to get overemphasized compared to the positive values until you hit the most negative value you defined, after which it just colours it as white regardless of what's on the colorbar.
The two work arounds I've found...
  1. Ensure the colormap only has as many levels as you want to use in the contours. This gets a bit tricky if the levels aren't evenly spaced though, and your caxis always needs to be as big or bigger than the data range.
  2. Contourf only positive values, create a new axes over the top of the old one, contourf the negative of the negative values (making them positive) and use a flipped version of the colormap. Then use "axis off" so you can see the positive values on the axes underneath.
It's a bit of a dog, but it works. Would be nice if there was a more elegant method, but I guess a function can be written for method 2 to avoid clogging up the code with multiple plots / flips.
Bjorn Gustavsson
Bjorn Gustavsson on 5 Oct 2020
OK, that was a bit more of a slog than I imagined. Have a look at the cmfit function of the colormap-and-colorbar file exchange contribution. (At least at a first glance) it seems to give you a function to automatically set the colormap-bands. (I haven't needed to do plots like yours, and haven't tried to use this function for that either...)
Also there ought to be a contourf-like contribution on the FEX that does this...

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!