MATLAB Answers

Plot matrix with columns of same color

45 views (last 30 days)
Rub Ron
Rub Ron on 31 Jul 2019
Edited: dpb on 1 Aug 2019
I have this plot:
Untitled.png
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

  0 Comments

Sign in to comment.

Accepted Answer

dpb
dpb on 31 Jul 2019
You mean
x=1:100;
y=randi(420,numel(x),4);
c={'r','g','b','k'};
figure
hAx=axes;
hold on
xlim([0 11])
arrayfun(@(i) plot(x(i),y(i,:),'o','color',c{i}),1:4)
something like
untitled.jpg
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.

  2 Comments

Rub Ron
Rub Ron on 31 Jul 2019
Thank for your answer too. Same here, I have tried your suggestion and it works, but the problem is that the original code plots the figure almost instantly, while with your suggestion it takes around 10 seconds to plot, and then the figure is slow to manipulate (dragging the fig, zooming in/out, etc).
dpb
dpb on 31 Jul 2019
Well, "there is no free lunch!"
You want speed or you want specific functionality more? :)
The problem is one of having to manually create the handles at the higher level to fool mother HG2.
I've not fully explored but you can try to get really, really clever and see if you can actually manage to fool it...
[r,c]=size(yy.'); % the row/columns really want to plot
hL=plot(nan(r,c),'o'; % create the desired number of line handles
x=ones(size(hL)); % the x vector base for each line
for i:c
set(hL(i),'XData',i*x,'YData',y(:,i));
end
and see how that performs.
You can, of course, use the cell array set syntax and do w/o the for...end loop but it's fairly complicated to construct.

Sign in to comment.

More Answers (1)

Adam Danz
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
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 Comments

Rub Ron
Rub Ron on 31 Jul 2019
Thank for your answer. I have tried your suggestion (the first one, with the loop) and it works, the problem is that the original code plots the figure almost instantly, while with your suggestion it takes around 10 seconds to plot, and then the figure is slow to manipulate (dragging the fig, zooming in/out, etc).
Adam Danz
Adam Danz on 31 Jul 2019
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.
dpb
dpb on 31 Jul 2019
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by