Plot matrix with columns of same color
20 views (last 30 days)
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
dpb on 31 Jul 2019
maybe? You'll have to define a group of colors of sufficient size or mod() the number you did define. To switch colors you'll need to recast so can keep the line handle of each line or retrieve the array of line handles from the .Children of the axes to be able to address them.
plot cycles colors by line and for a vector/array input it will plot each column as the line when they match in length; you need each row treated as the column but plot will internally override your orientation and draw against the x of the same length as the one vector. The single point x is a special case which is what the anonymous function make use of.
More Answers (1)
Adam Danz on 31 Jul 2019
Edited: Adam Danz on 31 Jul 2019
One option is put the plot within a loop where you loop through each unique x value.
xxUnq = unique(xx); % all unique xx values
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,:))
% * 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.