Reputation: 27
Different coloured points on a plot and to have the points in a legend with their respective colours.
I have created a struct which contains x, y values of points. With these points I hope to plot points on an image so I can see where they are. However, as I'm using a struct I can't get the plot to make the points in different colours. With the for loop I tried to coax matlab in creating multiple plots, and hopefully different lines, which I could then assign to different colours of my choosing.
Code:
img = imread('retinotopische map V1M Base clean.bmp');
hold on;
image([0.825 4.61],[-2.85 ,-8.25],img);
for A = 1:B
plot( [c(A).Lateral],[c(A).Bregma],'o','MarkerSize',10);
plot( [c(A).Lateral],[c(A).Bregma],'.','MarkerSize',10);
end
Upvotes: 2
Views: 218
Reputation: 21563
If you just want different colors, the solution is easier than you would expect:
Replace hold on
with hold all
and matlab will automatically use the next color.
Edit: If you start a new plot, you may need to call hold off
and hold all
again to get the right starting position.
Upvotes: 2
Reputation: 45752
You can change the colour using the 'Color' option:
img = imread('retinotopische map V1M Base clean.bmp');
hold on;
image([0.825 4.61],[-2.85 ,-8.25],img);
for A = 1:B
plot( [c(A).Lateral],[c(A).Bregma],'o','MarkerSize',10, 'Color', [A/B, 0, 1 - A/B]);
end
'Color' lets you specify an RGB triple like [1 0 0] for red for example. You can use this to plot whatever colors you want. My example will plot them in like a gradient but you could also have totally different colours using say rand(1, 3)
to get the triple? Or else make a colour matrix where you specify the exact order of colours you want like:
MyColours = [1 1 0;
0 0 1;
1 0 0;
0.5 0.2 0.9]; %etc...
and then in your for loop:
plot( [c(A).Lateral],[c(A).Bregma],'o','MarkerSize',10, 'Color', MyColours(A, :));
Upvotes: 1