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.
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
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.
@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)
