Link specific coordinates to a color when plotting them
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];
Accepted Answer
More Answers (1)
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
Vance Blake
on 8 Sep 2019
Edited: Vance Blake
on 8 Sep 2019
If you're just plotting scatter points, then yes, you can replace plot() with scatter() but you also need to check that the other inputs OK.
David Hill's answer might suit your needs better since it does not spit the data into different graphics handles.
Vance Blake
on 8 Sep 2019
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.
Categories
Find more on Annotations 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!