Airman01
Airman01

Reputation: 103

Filling markers when plotting in a loop

I am trying to plot 31 different vectors in a single figure in MATLAB. I am using a circular marker 'o', for plotting every vector and I want to have every vector painted in a different color and also I want to fill the markers as well with the same color as the markers edges.

I am using the following code:

while (n<=31)
   plot(x(n),y(n),'o',rand(1,3))  % Not filled markers
   n=n+1;
end

The problem is that since I am using a random for the color selection, when I try to run the following code:

while (n<=31)
   plot(x(n),y(n),'o','MarkerFaceColor',rand(1,3))  % Filled markers
   n=n+1;
end

The marker edge and the marker filling have different colors. I don't know how to fix this, maybe I shouldn't use the random for selecting the color but I don't know how to fix it, to obtain the same color in the marker edge and in the filling.

Upvotes: 0

Views: 756

Answers (2)

Suever
Suever

Reputation: 65430

I would recommend using scatter for this rather than creating n different plot objects that you then have to manage. Using the CData property it is possible to set a separate color for each of your data points.

colors = rand(31, 3);

x = rand(31,1);
y = rand(31,1);

s = scatter(x, y, 'filled', 'CData', colors);

enter image description here

Upvotes: 4

Peter
Peter

Reputation: 14937

'MarkerEdgeColor' is the property you want, in addition to FaceColor

while (n<=31)
   c = rand(1,3);
   plot(x(n),y(n),'o','MarkerFaceColor', c, 'MarkerEdgeColor', c);
   n=n+1;
end

Upvotes: 3

Related Questions