Link specific coordinates to a color when plotting them
4 views (last 30 days)
Show older comments
Hello I am wondering if there is a way to link specific coordinates in a matrix to always be plotted in a specific color. like if I had the set of coordinates shown below and i want the first 4 sets to always be plotted in red and the next 3 always plotted in green whenever they are being plotted so that even if I clear the axes and replot those coordinates will always appear in the color they were set to.
A = [38.8197976955836 -29.0434116907097
-37.0532684880158 0.644925865563445
-4.49735986053992 57.3402937422674
-43.7442096431328 38.5935144262550
41.5359946082739 41.4696332098067
57.3572679057450 8.87552592324304
-29.8320366435934 -43.1286701525403];
0 Comments
Accepted Answer
David Hill
on 8 Sep 2019
red=[1 0 0];
green = [0 1 0];
color=[red;red;red;red;green;green;green];%make color array for the colors you want at each index
scatter(A(:,1),A(:,2),[],color);%not sure what kind of plot you want
As long as the index does not change for the colors you want, then the above example should work.
1 Comment
David Hill
on 8 Sep 2019
You could also use a colormap to map what you want for your plotting colors.
More Answers (1)
Adam Danz
on 8 Sep 2019
Edited: Adam Danz
on 8 Sep 2019
When you use plot(x,y) it returns 1 handles and you cannot specify different colors to objects within the same handle.
You can use gscatter() (stats toolbox) to create grouped scatter points where you can specify the color (and size of markers) of each group.
h = gscatter(A(:,1),A(:,2),[1 1 1 1 2 2 2],[1 0 0; 0 1 0])
% _______________ here we define the groups by row
% ______________ here we define the colors for each group
If you don't have stats toolbox, you can separate the rows of A and plot them as individual objects.
h1 = plot(A(1:4,1),A(1:4,2), 'ro');
hold on
h2 = plot(A(5:end,1),A(5:end,2), 'go');
4 Comments
Adam Danz
on 8 Sep 2019
Yeah, both gscatter() and scatter() have pros and cons that vary based on the context of the problem. Based on your context (from another post), I think scatter() will keep your data more organized.
See Also
Categories
Find more on Discrete Data Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!