How do I make a plot with specific RGB color values for each marker from the same data set?
17 views (last 30 days)
Show older comments
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
0 Comments
Answers (1)
Chad Greene
on 20 Jun 2014
The scatter function accepts rgb color values corresponding to each point. It'll help you avoid using a loop.
2 Comments
Chad Greene
on 20 Jun 2014
Here's the code:
colors=[hsv(vrty)];
scatter(1:50,cos((1:50)*pi/50),30,colors,'filled')
Chad Greene
on 20 Jun 2014
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.
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!