Link specific coordinates to a color when plotting them

조회 수: 10 (최근 30일)
Vance Blake
Vance Blake 2019년 9월 8일
댓글: Adam Danz 2019년 9월 8일
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];

채택된 답변

David Hill
David Hill 2019년 9월 8일
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
David Hill
David Hill 2019년 9월 8일
You could also use a colormap to map what you want for your plotting colors.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Adam Danz
Adam Danz 2019년 9월 8일
편집: Adam Danz 2019년 9월 8일
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
Vance Blake
Vance Blake 2019년 9월 8일
@adam yeah im reading up on them both right now because i do like how the points are stored in separate groups but that might make removing them harder than necessary based on how this thread is going lol.
Adam Danz
Adam Danz 2019년 9월 8일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by