How do I make a plot with specific RGB color values for each marker from the same data set?

If I have an x and y vector and want to plot them with a specific RGB color value, how would I do that? Ideally, I would like to have a matrix of color values that would coincide with the x and y values. In addition, I was hoping to define x values with something like x=(-50:0.004:50) but I couldn't get MATLAB to plot the corresponding y and color values correctly.
I was initially thinking about using a for loop with hold on and plotting each data point separately, but that significantly slows down MATLAB when you have a lot of data points.
Here's a simplified example of the plot I am hoping to make.
hold on
vrty=50; %number of data points
colors=[hsv(vrty)]; %Defined colors I want to use
for i=1:vrty %loops figure until done with points
x=i;
y=cos(x*pi/vrty);
h=plot(x,y,'o');
cmatrix_size=size(colors);
color=colors((i),:);
set(h,'MarkerEdgeColor','none','MarkerSize',4,'MarkerFaceColor',color)
end

Answers (1)

The scatter function accepts rgb color values corresponding to each point. It'll help you avoid using a loop.

2 Comments

Here's the code:
colors=[hsv(vrty)];
scatter(1:50,cos((1:50)*pi/50),30,colors,'filled')
On a side note, as an alternative to the hsv color map, you can use rgbmap. I've included your example in the documentation file.

Sign in to comment.

Products

Asked:

on 20 Jun 2014

Edited:

on 21 Jun 2014

Community Treasure Hunt

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

Start Hunting!