Plotting a line with an array of marker colours?
25 ビュー (過去 30 日間)
古いコメントを表示
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/959855/image.png)
0 件のコメント
採用された回答
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 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!