Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Defining colors without plot or similiar command

143 views
Skip to first unread message

Riku

unread,
Dec 19, 2010, 10:20:29 AM12/19/10
to
How could I set the colororder for a plot of points when using a command like:
set(plotname,'xdata',x,'ydata',y)?
Using plot or scatter is too slow.
Not the real program, but what I tried for this problem:

x=[0 0]
y=[0 1]
colorscheme=[1 0 0;0 0 1]
graph=plot(x,y,'o')
set(gcf,'DefaultAxesColorOrder',colorscheme)
axis([0 100 0 200])
for i=1:100
x=[i i]
y=[i i+20]
set(graph,'xdata',x,'ydata',y)
drawnow
end

Thanks

Walter Roberson

unread,
Dec 19, 2010, 10:47:41 AM12/19/10
to

Color order is not used when you are updating the position of an
existing graphics object: the existing object color is used.

If you want to change the color of an existing object, write the code
yourself. For example,

thiscoloridx = 1 + mod(i,size(colorscheme,1));
set(graph, 'xdata', x, 'ydata', y, 'color', colorscheme(thiscoloridx,:));

Riku

unread,
Dec 19, 2010, 4:09:04 PM12/19/10
to
Thanks for the reply. This changes the color of the two points every other loop. How would I set one point as one color, and the other as another color?
Walter Roberson <robe...@hushmail.com> wrote in message <xOpPo.2345$OF6...@newsfe03.iad>...

Riku

unread,
Dec 20, 2010, 7:57:09 AM12/20/10
to
Defining the color with set(plotname,'color',1-by-3 color matrix) makes all the points the same color. Is there a low-level way of doing it like how plot or scatter defines the colors, as with an m-by-3 color matrix, each color row corresponding to each column of x and y values.
Thanks

Riku

unread,
Dec 21, 2010, 4:00:09 PM12/21/10
to
I think the way to do it is described in Matlab's "Help" in the section on "Setting multiple property values". But I don't understand the way Matlab operates when I do this:

prop_name(1)={'MarkerFaceColor'}
prop_values(1,1)={'r'}
prop_values(2,1)={'b'}
prop_values(3,1)={'k'}
prop_values(4,1)={'y'}
h=plot([0 0 0 0;1 1 1 1],[1 4 3 2;1 2 4 3],'o')
set(h,prop_name,prop_values)

and compare it to this:

prop_name(1)={'MarkerFaceColor'}
prop_values(1,1)={'r'}
prop_values(2,1)={'b'}
prop_values(3,1)={'k'}
prop_values(4,1)={'y'}
h=plot([0 0 0 0],[1 4 3 2],'o')
set(h,prop_name,prop_values)

??? Error using ==> set
Value cell array handle dimension must match handle vector length.

Error in ==> graphcolorstest at 7
set(h,prop_name,prop_values)

Why is it that in the first example, there are four handles, each having two markers, while in the second there is only one handle?
Thanks

Walter Roberson

unread,
Dec 21, 2010, 5:09:17 PM12/21/10
to

When plot() is handled a row matrix for X or Y, it converts it to a column
matrix, after which: plot() creates one line or lineseries object for each
column of Y.

[1 4 3 2;1 2 4 3]

has 4 columns for Y so plot() creates 4 lineseries objects.

[1 4 3 2] is a row vector so it is treated as the column vector [1;4;3;2]; Y
then has a single column so plot() creates a single lineseries object.

Riku

unread,
Dec 21, 2010, 5:58:04 PM12/21/10
to
> When plot() is handled a row matrix for X or Y, it converts it to a column
> matrix, after which: plot() creates one line or lineseries object for each
> column of Y.
>
> [1 4 3 2;1 2 4 3]
>
> has 4 columns for Y so plot() creates 4 lineseries objects.
>
> [1 4 3 2] is a row vector so it is treated as the column vector [1;4;3;2]; Y
> then has a single column so plot() creates a single lineseries object.

I see. Does this mean that it is impossible to create a plot with the number of handles equal to the number of plot points(except when there is a single point)? As:

plot([0 ;0],[1; 4],'o')
has only one handle.

Walter Roberson

unread,
Dec 21, 2010, 6:13:53 PM12/21/10
to

No, it just means that plot() is not the tool to do that with.

If you are wanting to create a plot with one handle per point-pair, then each
point-pair would only cover one point, and there would be no joining of
points. In that case you might as well use scatter(), which allows you to
specify the color of each point, and you can change the color of points
afterwards by changing the CData property of the scattergroup series object.

If for some reason one handle per point-pair is important, line() the handles
in to existence one by one, such as via

h = arrayfun(@line, X, Y);

Kelly Kearney

unread,
Dec 21, 2010, 6:15:21 PM12/21/10
to
"Riku" wrote in message <ierbds$rp4$1...@fred.mathworks.com>...

Add a row of NaNs to your data; they will force plot to treat each column as a separate object.

x=[0 0];
y=[0 1];

x = [x; nan(size(x))];
y = [y; nan(size(y))];

h = plot(x,y,'o');
col = {'r';'b'};
set(h, {'color'}, col);

0 new messages