Plot matrix with columns of same color
이전 댓글 표시
I have this plot:

I would like to have all the points of the same x-axis to be of the same color, and to change the color (as desired) from one x-axis to the next one, so that I have columns of different colors. Currently, the plot is generated by this code:
figure; hold on;
grid on; grid minor;
plot(xx, yy, 'o', 'MarkerSize', 2);
Where xx is a vector of 1x255 and yy is a matrix of 255x100. I have been struggling to find an answer for this, so any hint would be really appreciated
채택된 답변
추가 답변 (1개)
One option is put the plot within a loop where you loop through each unique x value.
xxUnq = unique(xx); % all unique xx values
hold on
cdata = jet(numel(xxUnq)); %create your color matrix
for i = 1:numel(xxUnq)
idx = xx == xxUnq(i);
plot(xx(idx),yy(idx,:), 'o', 'Color', cdata(i,:))
end
% * Not tested
Another option is to use group-scatter where xx is the grouping variable.
h = gscatter(xx',yy, xx');
"h" is a vector of handles, one for each marker. You can change the colors after plotting. This may take some time since you've got a lot of data and each point will be an independent object.
댓글 수: 3
Rub Ron
2019년 7월 31일
As dpb mentioned, that's the cost of plotting indpendent objects. The loop splits the data up into groups and each group gets its own object handle. The gscatter() produces a handle for each marker. That's going to slow down rendering and increase the file size.
Both ways are plotting individual objects -- the array version creates 100 line handles instead of 255, though, that are needed to control line color on each x as desired.
Doesn't seem that should make the kind of slowdown he's reporting, though....
I don't know what other shortcuts plot() can take when it's an array call vis a vis N vectors, though; there may be some other behind the curtain the great and powerful Oz of HG2 tricks happening.
That's the idea behind the alternate of creating all the handles in one swell foop and to set the data arrays then instead. One could also 'spearmint if whether it matters if try to create at the right size initially or not.
카테고리
도움말 센터 및 File Exchange에서 Color and Styling에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
