How to set contourf colors ?

I use contour for plot three concentric isolines, now I would like to fill the area inside this isolines with a choosen color, for example heavy green, medium green and light green. How I can do it?
I tried with contourf and colormap but I'm not enought skilled for properly set the colors. This is my code:
data = fopen ( datafile,'r');
M = fscanf ( data , '%f', [128, 128]);
[x,y] = meshgrid(-gr:2*gr/127:gr);
contourf(x,y,M,[5000 1000 500]);
map = [0 0.6 0.2; 0 0.8 0.2; 0 1 0.2];
colormap (map);
Any helps?

 Accepted Answer

Kelly Kearney
Kelly Kearney on 24 Sep 2014
Edited: Kelly Kearney on 24 Sep 2014
I wrote the contourfcmap function to do exactly that... much more reliable than trying to fiddle with colormaps and color limits.

More Answers (4)

doc caxis
And a simple example:
contourf(magic(5),3);
caxis([0 25])
colormap(rand(3))
Dario
Dario on 24 Sep 2014
Doesn't works:
data = fopen ( datafile,'r');
M = fscanf ( data , '%f', [128, 128]);
[x,y] = meshgrid(-gr:2*gr/127:gr);
contourf(x,y,M,[5000 1000 500]);
caxis([0 25])
colormap(rand(3))
I see all blu.

1 Comment

I used caxis([0 25]) because my example data ranged from 1:25.
You might want to use something like:
caxis([min(M(:)),max(M(:))])
to scale it to the range of your data

Sign in to comment.

Dario
Dario on 24 Sep 2014
Thanks for your replies guys but I'm a beginner so I need more deep information.
@ Sean: I know about the Caxis command that set the range on colormap, I tried but it give me always only one color. I stated my code with 3 areas so can you give an istruction that works in my case? And where I find the range number I must set for the three color tone I want to set?
@ Kelly: contourfcmap seems more fast, can you write me an example that works with my code?
Thanks a lot for time and help.

7 Comments

Well, I don't have your data; you'll have to attach your datafile if you want specifics. But here's an example with placeholder data. Note that you need to define one more contour level than number of colors, since the colors refer to the space between two contour lines.
[x,y,M] = peaks(50);
clev = [-5 -1 1 5];
map = [0 0.6 0.2; 0 0.8 0.2; 0 1 0.2];
contourfcmap(x,y,M,clev,map,[0 0 0], [1 1 1], 'eastoutside');
Contourfcmap is supported in version 7.10 (R2010a)?
Kelly Kearney
Kelly Kearney on 25 Sep 2014
Edited: Kelly Kearney on 25 Sep 2014
Yes, it should be fine in that version. Are you encountering problems? I've hit occasional issues where contourf creates some unexpected lines, but contourfcmap should throw an error stating this.
Kelly thanks for your function but I have a trouble. I tried this code:
clev = [500 1000 5000];
cmap = [0 0.6 0.2; 0 0.8 0.2; 0 1 0.2];
contourfcmap (x,y,Data, clev, cmap);
Matlab give ma an error: cmap must be nlev-1 x 3 colormap array. why?
As I stated above, you need to provide one more contour line level than the number of colors. Right now, you've got 3 colors, and only 2 intervals to assign those to (500-1000 and 1000-5000). Based on your screenshot, I'm guessing you want the third color to fill in anything higher than 5000. In that case, pass 2 colors as the colormap, and the third as the hi input:
contourfcmap(x,y,Data,clev,cmap(1:2,:),[1 1 1],cmap(3,:));
Dario
Dario on 26 Sep 2014
Edited: Dario on 26 Sep 2014
Now it works! Thank you Kelly. And if I want to display levels text? With contour I wrote:
contourf(x,y,Dati,clev,'ShowText','on')
What about contourfcmap?
That will depend on which version of Matlab you're running, and whether you have the Mapping Toolbox (those two factors determine whether contourfcmap output is contourgroup objects or patch and line objects. Either way, the easiest would likely be to just add new contours on top and label those:
contourfcmap (x,y,Data, clev, cmap);
hold on;
[c,h] = contour(x,y,Data,clev, 'k');
clabel(c,h);

Sign in to comment.

Dario
Dario on 25 Sep 2014
Edited: Dario on 25 Sep 2014
This is my code that plot three concentric ellipses:
map = [0 0.6 0.2; 0 0.8 0.2; 0 1 0.2];
colormap (map);
contourf(x,y,Data,[5000 1000 500]);
Why the two external ellipses have the same color?
And if I add the code
caxis([500,5000]);
I have the same result, only two different colors.

Categories

Tags

Community Treasure Hunt

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

Start Hunting!