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
采纳的回答
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 个评论
Dear Adam Thank you so much for your answer.
Thanks Adam, I accepted the answer.
It appears as an unaccepted answer. The blue "accpet this answer" button will accept the answer.
I accepted the answer yesterday and I can’t see the accept button anymore. The answer wasn’t accepted yet?
Hmmm it's ok. Maybe there's a glitch. It's not that important, no worries :) Glad I could help out.
@yaser obviously you haven’t logged into your old account that’s why you’re not able to see the accept button.
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.
@Adam I had to log out and log in again to fix this. Thanks a lot for your help :)
Thanks Yaser & Madhan!
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
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.
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 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
