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

19 ビュー (過去 30 日間)
ryan eckman
ryan eckman 2021 年 9 月 14 日
回答済み: Ashutosh Singh Baghel 2021 年 9 月 20 日
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?
  1 件のコメント
KSSV
KSSV 2021 年 9 月 14 日
scatter takes Cdata to show up colors right? USe that and set your colormap.

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

回答 (1 件)

Ashutosh Singh Baghel
Ashutosh Singh Baghel 2021 年 9 月 20 日
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.

カテゴリ

Help Center および File ExchangeScatter Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by