Clear Filters
Clear Filters

Plotting a line with an array of marker colours?

25 views (last 30 days)
Hi.
I'm trying to create a 'bubble plot', where the colour of each point made by the plot is determined by the value of a third variable.
My current method of generating this plot is to modify the colour of each point one by one, using a for loop. This process works fine, but it's rather slow.
I'm wondering if its possible to enter these colours all at once as a sort of value array or colour map, such that I don't need to plot each point individually. Is this achieveable?
Below is a bit of sample code that is similar to what I'm using (I simply made fake variable names and lowered the sample size, the sample size used by my actual data is about 100 times larger).
I've also attached an image of the resulting figure.
Any help would be greatly appreciated!
x=[1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5]';
y=[1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5]';
z=x.*y;
%Generates and stores the colour scale for each point.
R=(abs(z.*(z-max(z)).*(z-min(z))))/max(abs(z.*(z-max(z)).*(z-min(z))));
G=1-abs(0.5*(1-sign(z).*((abs(z)-min(abs(z)))/((max(abs(z))-min(abs(z)))))));
B=1-(abs(z)-min(abs(z)))/((max(abs(z))-min(abs(z))));
Plot_Colour=[R,G,B];
figure(1)
%plots each point individually and assigns it a given colour
for i=1:length(x);
plot(x(i),y(i),'color',[0 0 0],'Marker','s','MarkerSize',10,'MarkerFaceColor',Plot_Colour(i,1:3));
hold on
end

Accepted Answer

Chunru
Chunru on 11 Apr 2022
You can use scatter.
x=[1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5]';
y=[1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5]';
z=x.*y;
%Generates and stores the colour scale for each point.
R=(abs(z.*(z-max(z)).*(z-min(z))))/max(abs(z.*(z-max(z)).*(z-min(z))));
G=1-abs(0.5*(1-sign(z).*((abs(z)-min(abs(z)))/((max(abs(z))-min(abs(z)))))));
B=1-(abs(z)-min(abs(z)))/((max(abs(z))-min(abs(z))));
Plot_Colour=[R,G,B];
figure(1)
%plots each point individually and assigns it a given colour
scatter(x, y, 36, Plot_Colour, 'filled', 's')

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!