How to I plot a vector whose points change in color based on the value of another vector?

37 views (last 30 days)
I have two 4000x1 vectors, call them A and B. I want to plot A on a graph, and I want the color to change based on the value of B. For example, if B(1) is between 0.95-1, I want to plot A(1) in dark red. If B(1) is between 0.9-0.949, I want to plot A(1) in a lighter shade of red. I want the colors to change from dark red at the max to light blue at the minimum value of B.
I initially tried this with a for loop and conditional statements (if B(i) > 0.95, scatter(x(i),A(i),1,'r'), but I want more color variation, and using the color short names doesn't provide intensity on the color. Is there a way this can be done without brute forcing it using hexadecimal color codes?

Answers (1)

Ashutosh Singh Baghel
Ashutosh Singh Baghel on 20 Sep 2021
Hi Ryan, I understand you wish to use the 'scatter' function to plot 'A' and change color of data points based on value of 'B'. Please refer to the example shown below.
n = 4000;
A = 1:n;
B = rand(1,n);
r = linspace(1,0,n);
c = [r' zeros(n,2)];
scatter(A,B,[],c);
Please feel free to refer to the documentation page on 'scatter' function.

Community Treasure Hunt

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

Start Hunting!