フィルターのクリア

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')
  1 件のコメント
Matthew Schipper
Matthew Schipper 2022 年 4 月 11 日
Awesome, just what I was looking for. Cheers!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File Exchange2-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