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

 Akzeptierte Antwort

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 Kommentare

William McNair
William McNair am 17 Nov. 2021
Bearbeitet: William McNair am 17 Nov. 2021
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!
.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Color and Styling finden Sie in Hilfe-Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by