Hello,
I have 3 arrays of the same length X,Y,intensity.
I would like to plot X and Y with 'x' markers, but I also want to change the color of the marker based on the intensity array (0-100).
Is there an easy way to do this in MATLAB?
Many thanks

 채택된 답변

Yes.
x = rand(100,1);
y = rand(100,1);
intensity = randi(99,100,1);
figure
scatter(x, y, 50, intensity, '+')
grid
colormap(turbo)
colorbar
.

댓글 수: 4

William McNair
William McNair 2021년 11월 17일
편집: William McNair 2021년 11월 17일
Thanks so much! Is it possible with same color but transparacy changes? Or color varies from light to dark?
As always, my pleasure!
The transparency is generally known as the ‘alpha’ property, and scatter allows MarkerEdgeAlpha and MarkerFaceAlpha to be specified.
The colour varying from light to dark is dependent on the colormap being used. See Input Arguments for a list of available colormaps.
It is also possible to create custom colormaps, for example in the second and third scatter calls —
cm = zeros(25,3);
cm(:,1) = linspace(0.5, 0.75, size(cm,1)).';
figure
hs = scatter(rand(100,1), rand(100,1), 150, randi(50, 100, 1), 'filled', 'p');
hs.AlphaData = 3.5*exp(-(hs.XData - 0.5).^2); % Create Gaussian Transparency Vector Based On X-Coordinate
hs.MarkerFaceAlpha = 'flat';
grid
colormap(turbo)
colorbar
title('Transparency Experiment')
figure
scatter(rand(100,1), rand(100,1), 150, randi(50, 100, 1), 'x')
grid
colormap(cm)
colorbar
title('Custom Colour Experiment #1')
figure
scatter(rand(100,1), rand(100,1), 150, randi(50, 100, 1), 'filled', 'p')
grid
colormap(cm)
colorbar
title('Custom Colour Experiment #2')
This creates a gradient red colormap (experiment with the first two arguments in the linspace call to change it) that scatter then uses to colour the markers. This works better with markers that have faces to fill, however it will work with all of them. These are simply two examples.
.
This is extactly what I needed. Thanks again. The matlab comunity is great.
As always, my pleasure!
Thank you for all of us!
.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Color and Styling에 대해 자세히 알아보기

태그

질문:

2021년 11월 17일

댓글:

2021년 11월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by