help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: setting 'color' for a plot with multiple lines


From: Ben Abbott
Subject: Re: setting 'color' for a plot with multiple lines
Date: Mon, 21 Jan 2013 08:05:36 -0500

On Jan 21, 2013, at 4:15 AM, address@hidden wrote:

> On Sun, Jan 20, 2013 at 04:35:49PM -0500, Ben Abbott wrote:
>> 
>> On Jan 20, 2013, at 3:58 PM, address@hidden wrote:
>> 
>>> Dear all,
>>> 
>>> I want to set the colors to some standard without cycling through each of 
>>> them manually or via a for-loop.
>>> 
>>> The following script show my attempt and I do not understand why the last 
>>> one doesn't work.
>>> (some plot() commands are commented to not let Octave stop at it).
>>> 
>>> Can someone explain why the bottom plot() command doesn't work when I feed 
>>> it a proper cell?
>>> 
>>> --------------------------------------------------
>>> 
>>> x=[linspace(0,2,30)' , linspace(0,2,30)'];
>>> y=x;
>>> y(:,1)=sin(x(:,1));
>>> y(:,2)=cos(x(:,2));
>>> 
>>> figure
>>> p=plot(x,y);
>>> standardcolors=get(p,'color')
>>> % this works, but the colors are not what I want
>>> 
>>> figure
>>> K=rainbow(size(y)(2));
>>> %p=plot(x,y,'color',K);
>>> %% this doesn't work. Because K is a matrix?                                
>>>                                                                             
>>>                                 
>>> 
>>> figure
>>> KK=mat2cell(K,ones(1,size(K)(1)),3)
>>> whos standardcolors KK
>>> 
>>> %plot(x,y,'color',KK)
>>> % why doesn't this work?
>>> 
>>> figure
>>> p=plot(x,y)
>>> get(p,'color')
>>> set(p,'color',KK)
>>> 
>>> --------------------------------------------------
>>> 
>>> many thanks,
>>> 
>>> indium
>> 
>> The syntax you're attempting is not valid.  The set() command isn't specific 
>> to a particular object type.  Thus, properties can have values that belong 
>> to any class ... including cells.  
>> 
>> You can get the result you want with the line below.
>> 
>>      arrayfun (@(n) set (p(n), 'color', KK{n}), 1:numel(p));
>> 
>> Ben
> 
> Dear Ben,
> 
> thank you for the reply and the solution.
> 
> If the arrayfun() is the solution, then I suppose I can skip the conversion 
> from matrix K to cell KK and simply insert K(n,:) in arrayfun(), instead of 
> K{n}. 
> 
> For my understanding: the get() command outputs a cell, which can not be used 
> as an input for set(). What is the reason for not having set() input and 
> get() output in the same format? 

That is not the reason. If a property may contain a cell (UserData for example) 
then how should the parser handle ...

        set ([h1, h2], "UserData", {"foo", "bar"})

Is that equivalent to 

        set (h1, "UserData", {"foo", "bar"})
        set (h2, "UserData", {"foo", "bar"})

or

        set (h1, "UserData", "foo")
        set (h2, "UserData", "bar")

The parsers in Octave and Matlab function according to the first.

Which means that set() and get() are not reciprocal functions.  To illustrate, 
for the first version ...

        get ([h1, h2], "UserData")  --> {{"foo", "bar"}, {"foo", "bar"}}

and for the second

        get ([h1, h2], "UserData")  --> {"foo", "bar"}

Something close to what you're looking for is ..

        set ([h1, h2], {"UserDtata", "UserData"}, {"foo", "bar"})

Which is equivalent to ...

        set (h1, "UserData", "foo")
        set (h2, "UserData", "bar")

This may imply that the following should work

        plot (1:10, {"color"}, {[1 0 1]})

But it currently does not (I'm not sure it should ... need to think about it 
further).  Perhaps a bug report should be filed just in case.

        https://savannah.gnu.org/bugs/index.php?38131

Ben





reply via email to

[Prev in Thread] Current Thread [Next in Thread]