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?

 Respuesta aceptada

Kelly Kearney
Kelly Kearney el 24 de Sept. de 2014
Editada: Kelly Kearney el 24 de Sept. de 2014

3 votos

I wrote the contourfcmap function to do exactly that... much more reliable than trying to fiddle with colormaps and color limits.

Más respuestas (4)

Sean de Wolski
Sean de Wolski el 24 de Sept. de 2014

0 votos

doc caxis
And a simple example:
contourf(magic(5),3);
caxis([0 25])
colormap(rand(3))
Dario
Dario el 24 de Sept. de 2014

0 votos

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 comentario

Sean de Wolski
Sean de Wolski el 24 de Sept. de 2014
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

Iniciar sesión para comentar.

Dario
Dario el 24 de Sept. de 2014

0 votos

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 comentarios

Kelly Kearney
Kelly Kearney el 24 de Sept. de 2014
Editada: Kelly Kearney el 24 de Sept. de 2014
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');
Dario
Dario el 25 de Sept. de 2014
Contourfcmap is supported in version 7.10 (R2010a)?
Kelly Kearney
Kelly Kearney el 25 de Sept. de 2014
Editada: Kelly Kearney el 25 de Sept. de 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.
Dario
Dario el 25 de Sept. de 2014
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?
Kelly Kearney
Kelly Kearney el 26 de Sept. de 2014
Editada: Kelly Kearney el 26 de Sept. de 2014
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 el 26 de Sept. de 2014
Editada: Dario el 26 de Sept. de 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?
Kelly Kearney
Kelly Kearney el 29 de Sept. de 2014
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);

Iniciar sesión para comentar.

Dario
Dario el 25 de Sept. de 2014
Editada: Dario el 25 de Sept. de 2014

0 votos

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.

Categorías

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

Etiquetas

Preguntada:

el 24 de Sept. de 2014

Comentada:

el 29 de Sept. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by