Scatter plot with a color variation based on a third vector

I have three variables (Return, Risk, Supply) where I would like to present them on a scatter plot as the attached. I would like to plot them based on the two variables (Return and Risk) and I would like to color them based on the third variable (supply). Now I want to keep the color varying and I would like to make any value of the supply that is higher than 4800 blue and any value that is less than 4000 red? Anyway to help. I would like to add the colorbar and legend next to that. I have attached my data and code as below
pointsize = 100;
figure
scatter(Return,Risk, pointsize,Supply, 'filled')
ylim([0 35])
% colormap(jet(25))
% caxis([0 max([z1(:);z2(:)])])
colorbar;
title('Efficient Frontier Focusing on Fields [1,2,8] Producing on Year 1');
xlabel('Expected Risk, B$') % x-axis label
ylabel('Expected NPV, B$') % y-axis label

 採用された回答

Adam Danz
Adam Danz 2019 年 5 月 17 日
編集済み: Adam Danz 2019 年 5 月 18 日

2 投票

Use gscatter() (grouped scatter)
%Define thesholds
thresholds = [4000, 4800];
pointsize = 30;
group = ones(length(Supply),1).*2;
group(Supply < thresholds(1)) = 1;
group(Supply > thresholds(2)) = 3;
groupColors = [0 0 1; 0 0 0; 1 0 0];
figure()
gscatter(Return,Risk,group,groupColors,'.',pointsize,'filled')
ylim([0 35])
title('Efficient Frontier Focusing on Fields [1,2,8] Producing on Year 1');
xlabel('Expected Risk, B$') % x-axis label
ylabel('Expected NPV, B$') % y-axis label
% Create colorbar
colormap(groupColors)
chb = colorbar();
caxis([0,1])
set(chb,'Ticks',0.1667:0.3334:1,'TickLabels',{'sup<4000', 'between', 'sup>4800'})
Or use scatter() and define color of each plot
%Define thesholds
thresholds = [4000, 4800];
% Assign color
colorID = zeros(length(Supply),3); % default is black
colorID(Supply < thresholds(1),3) = 1; %blue
colorID(Supply > thresholds(2),1) = 1; %red
% your code, slightly adapted
pointsize = 100;
figure
scatter(Return,Risk,pointsize,colorID,'filled');
ylim([0 35])
title('Efficient Frontier Focusing on Fields [1,2,8] Producing on Year 1');
xlabel('Expected Risk, B$') % x-axis label
ylabel('Expected NPV, B$') % y-axis label
% Create colorbar
colormap([0 0 1;0 0 0;1 0 0])
chb = colorbar();
caxis([0,1])
set(chb,'Ticks',0.1667:0.3334:1,'TickLabels',{'sup<4000', '', 'sup>4800'})

12 件のコメント

Yaser Khojah
Yaser Khojah 2019 年 5 月 18 日
Dear Adam Thank you so much for your answer.
Yaser Khojah
Yaser Khojah 2019 年 5 月 18 日
Thanks Adam, I accepted the answer.
Adam Danz
Adam Danz 2019 年 5 月 19 日
It appears as an unaccepted answer. The blue "accpet this answer" button will accept the answer.
Yaser Khojah
Yaser Khojah 2019 年 5 月 19 日
I accepted the answer yesterday and I can’t see the accept button anymore. The answer wasn’t accepted yet?
Adam Danz
Adam Danz 2019 年 5 月 19 日
Hmmm it's ok. Maybe there's a glitch. It's not that important, no worries :) Glad I could help out.
madhan ravi
madhan ravi 2019 年 5 月 19 日
編集済み: madhan ravi 2019 年 5 月 19 日
@yaser obviously you haven’t logged into your old account that’s why you’re not able to see the accept button.
Yaser Khojah
Yaser Khojah 2019 年 5 月 19 日
編集済み: Yaser Khojah 2019 年 5 月 19 日
I'm not really sure how this happend. I only have one account. Let me know if there is another way to fix this. Sorry. I'm confused too since I can see that I can answe the quesiton.
Yaser Khojah
Yaser Khojah 2019 年 5 月 19 日
@Adam I had to log out and log in again to fix this. Thanks a lot for your help :)
Adam Danz
Adam Danz 2019 年 5 月 19 日
Thanks Yaser & Madhan!
nicole
nicole 2023 年 2 月 27 日
Hi Adam!
Thank you so much for this, it's super helpful!
Quick question, is there a way to change the color that you are assigning the dots?
Thank you
Adam Danz
Adam Danz 2023 年 2 月 27 日
Thanks @nicole, if you're using the gscatter solution, the colors are set in the groupColors variable. If you're using the scatter solution, the colors are set using the colormap. Both are demonstrated in my answer.
nicole
nicole 2023 年 3 月 2 日
This solution does not necessarily work. While it does parse out the supply variable according to the thresholds - less than 4000, between supply 4000-4800, and above supply 4800, and it does returns a matrix (ColorID) of 0 or 1's, 0 for false, 1 for true in each of the three columns, it will not actually color code your points according to the thresholds you set. Instead, for ColorID it will return a matrix of size (length(Supply), 3). In the first column of ColorID you will get a 0 for all of the values that aren't below 4000 in your supply variable, and a 1 for all the variables that are, this will signal on the graph that this point should be blue. In the second column of ColorID you will get a variable with all values of Supply between 4000-4800 being 1, and the rest being 0, this signals the default (black) becuase we did not set that to any color. The third column of ColorID will be all of the values in the supply variable that are above 4800, if the supply is not above 4800 there will be a 0, if the supply is above 4800 there will be a 1.
However, all this does is return a variable (ColorID) with a bunch of 0's and 1's, corresponding to reds and blues that work for the Supply variable, but when you are plotting the graph the indices of ColorID are plotting colors according to the indices of the RETURN and RISK variable.
So say the supply, risk, and return vector are of size (100, 1), and consequently the colorID vector is of size (100, 3). Say at supply indice (80, 1) the supply values is 4800, this means that the colorID indice (80,1) will be 1 while colorID indices (80,2) and (80,3) will be 0.
Now we go to the plotting part, scatter(Return,Risk,pointsize,colorID,'filled');. Let's say the return value at (80,1) is 5, and a return value of 5 corresponds to a risk value of 5. Say that risk and return have a supply value of 5 at (80,1). REGARDLESS of the fact that the color indice value has a value of 1 at indice (80,1) according to the supply vector corresponding to a supply value of 4800, the return and risk values may have a different supply value linked to it, but the point will still be red due to the COLORID vector.

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

その他の回答 (0 件)

カテゴリ

質問済み:

2019 年 5 月 17 日

コメント済み:

2023 年 3 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by