Scatter plot with a color variation based on a third vector

66 views (last 30 days)
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

Accepted Answer

Adam Danz
Adam Danz on 17 May 2019
Edited: Adam Danz on 18 May 2019
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 Comments
Adam Danz
Adam Danz on 27 Feb 2023
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 on 2 Mar 2023
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Colormaps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!