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 件のコメント
Yaser Khojah
2019 年 5 月 18 日
Dear Adam Thank you so much for your answer.
Yaser Khojah
2019 年 5 月 18 日
Thanks Adam, I accepted the answer.
Adam Danz
2019 年 5 月 19 日
It appears as an unaccepted answer. The blue "accpet this answer" button will accept the answer.
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
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
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.
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
2019 年 5 月 19 日
Thanks Yaser & Madhan!
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
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 件)
カテゴリ
ヘルプ センター および 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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
