Thread Subject: colors for contour plots

Subject: colors for contour plots

From: Ernesto molina

Date: 23 Sep, 2009 07:00:07

Message: 1 of 5

hi there,
I.m doing some contour plots (contourf) and I will llike to assign specific colors for value ranges....the colormap editor does not allow me to choose specificly that I want for example:
dark red: > +12.5m
mid red: +7.5 < X <= +12.5 m
light red: +2.5 m < X <= +7.5 m

any help will be very appreciated
Cheers
Ernesto

Subject: colors for contour plots

From: Sebastiaan

Date: 23 Sep, 2009 10:09:04

Message: 2 of 5

"Ernesto molina" <onka72@hotmail.com> wrote in message <h9ch1n$121$1@fred.mathworks.com>...
> hi there,
> I.m doing some contour plots (contourf) and I will llike to assign specific colors for value ranges....the colormap editor does not allow me to choose specificly that I want for example:
> dark red: > +12.5m
> mid red: +7.5 < X <= +12.5 m
> light red: +2.5 m < X <= +7.5 m
>
> any help will be very appreciated
> Cheers
> Ernesto

As far as I know, you can only change the contour fill colour afterwards, not specify it while plotting.

Example using peaks:
[C,h] = contourf(peaks(20));
Cld = get(h, 'Children');
for j=1:length(Cld)
    if strcmp(get(Cld(j), 'Type'), 'patch')
        Iso = get(Cld(j), 'CData');
        if Iso<-5
            set(Cld(j), 'FaceColor', [1 0 0]);
        elseif Iso>=-5 && Iso<0
            set(Cld(j), 'FaceColor', [0 1 0]);
        elseif Iso>=0 && Iso<5
            set(Cld(j), 'FaceColor', [0 0 1]);
        else
            set(Cld(j), 'FaceColor', [0 1 1]);
        end
    end
end
    
Will this work for you?

Sebastiaan

Subject: colors for contour plots

From: Jerome Briot

Date: 23 Sep, 2009 11:58:06

Message: 3 of 5

> As far as I know, you can only change the contour fill colour afterwards, not specify it while plotting.
> ...
> Sebastiaan

The same solution but with a colormap :

figure

map = [1 0 0
       0 1 0
       0 0 1
       0 1 1];
   
colormap(map);

[C,h] = contourf(peaks(20));
Cld = get(h, 'Children');
for j=1:length(Cld)
    if strcmp(get(Cld(j), 'Type'), 'patch')
        Iso = get(Cld(j), 'CData');
        if Iso<-5
            set(Cld(j), 'cdata', 0);
        elseif Iso>=-5 && Iso<0
            set(Cld(j), 'cdata', 1);
        elseif Iso>=0 && Iso<5
            set(Cld(j), 'cdata', 2);
        else
            set(Cld(j), 'cdata', 3);
        end
    end
end

Jerome

Subject: colors for contour plots

From: Ernesto molina

Date: 24 Sep, 2009 03:56:04

Message: 4 of 5

Thanks for that, I'm closer to get what a wanted but:
map =

         0 0 0.5608
         0 0.2510 1.0000
         0 1.0000 1.0000
    1.0000 1.0000 1.0000
    1.0000 0.3765 0
    1.0000 0 0
    0.7843 0 0
 
creating my 7 colors (range)
then colormap(map)
...[C,h] = contourf(long, time,'my variable');% my variable is 540x128
Cld = get(h, 'Children');
for j=1:length(Cld)
if strcmp(get(Cld(j),'Type'),'patch')
Iso=get(Cld(j),'CData');
if Iso > 12.5
set(Cld(j),'cdata',5);
elseif Iso >7.5&& Iso <= 12.5
set(Cld(j),'cdata',4);
elseif Iso >2.5&& Iso <= 7.5
set(Cld(j),'cdata',3);
elseif Iso >= -2.5 && Iso <= 2.5
set(Cld(j),'cdata',0);
elseif Iso < -2.5 && Iso >= -7.5
set(Cld(j),'cdata',1);
elseif Iso < -7.5 && Iso >= -12.5
set(Cld(j),'cdata',2);
elseif Iso < -12.5
end
end
end

..what I dont get is where you assign the color, I mean when doing set(Cld(j),'cdata', #).
I hope you understand my sort of explanation
cheers and thank you again
Ernesto
"Jerome Briot" <dut@matlab.fr> wrote in message <h9d2gd$76h$1@fred.mathworks.com>...
> > As far as I know, you can only change the contour fill colour afterwards, not specify it while plotting.
> > ...
> > Sebastiaan
>
> The same solution but with a colormap :
>
> figure
>
> map = [1 0 0
> 0 1 0
> 0 0 1
> 0 1 1];
>
> colormap(map);
>
> [C,h] = contourf(peaks(20));
> Cld = get(h, 'Children');
> for j=1:length(Cld)
> if strcmp(get(Cld(j), 'Type'), 'patch')
> Iso = get(Cld(j), 'CData');
> if Iso<-5
> set(Cld(j), 'cdata', 0);
> elseif Iso>=-5 && Iso<0
> set(Cld(j), 'cdata', 1);
> elseif Iso>=0 && Iso<5
> set(Cld(j), 'cdata', 2);
> else
> set(Cld(j), 'cdata', 3);
> end
> end
> end
>
> Jerome

Subject: colors for contour plots

From: Rune Allnor

Date: 24 Sep, 2009 07:16:43

Message: 5 of 5

On 23 Sep, 09:00, "Ernesto molina" <onk...@hotmail.com> wrote:
> hi there,
> I.m doing some contour plots (contourf) and I will llike to assign specific colors for value ranges....the colormap editor does not allow me to choose specificly that I want for example:
> dark red:        > +12.5m
> mid red:    +7.5 < X <= +12.5 m
> light red:     +2.5 m < X <= +7.5 m

First of all, set up a color map for the colors you want:

ncolors = 5;
cmap =zeros(ncolors,3);
cmap(:,1)=1;
cmap(:,2:3)=flipud((0:(ncolors-1))'*[1,1]/(ncolors-1));

Then plot the data with specified contour levels (note that
you need as many contour levens as colors in the color map):

data=rand(10);
contourf(data,[0,1,2]/3)

Last, activate the color map you made:

colormap(cmap)

And ther you are.

Rune

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
colormap Ernesto molina 23 Sep, 2009 03:04:04
contourf Ernesto molina 23 Sep, 2009 03:04:04
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com