필터 지우기
필터 지우기

Plotting a line with an array of marker colours?

조회 수: 25 (최근 30일)
Matthew Schipper
Matthew Schipper 2022년 4월 11일
댓글: Matthew Schipper 2022년 4월 11일
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

채택된 답변

Chunru
Chunru 2022년 4월 11일
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')

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by