How can I make my 'x' matrix in scatterplot in 2 different colors??

9 ビュー (過去 30 日間)
Zhou Ci
Zhou Ci 2023 年 2 月 2 日
コメント済み: Walter Roberson 2023 年 2 月 3 日
Hello everyone,
I am trying to make a scatterplot of below data. Where temp should be on y-axis and rest of the five bins on x-axis. Regarding x-axis, I want 2nd column (Bin 1) to be in a different color and rest of the 4 bins in another color. I can't figure out how to do this. Any help would be highly appreciated. Thank you.

採用された回答

Arif Hoq
Arif Hoq 2023 年 2 月 2 日
編集済み: Arif Hoq 2023 年 2 月 2 日
temp=[-37; -19; -24; -14];
bin1=[0.0416;0.046667;0.044463;0.024943];
bin2=[0.107853;0.120159;0.114775;0.063871];
bin3=[0.071318;0.078653;0.075131;0.042547];
% bin1 plot
scatter(bin1,temp,'o','filled')
hold on
plot(bin1,temp,'ro','MarkerFaceColor','r')
% bin2 and bin3 plot
scatter([bin2 bin3], temp,'o','filled')
hold on
plot([bin2 bin3], temp,'go','MarkerFaceColor','g')

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2023 年 2 月 2 日
A different approach would be to use gscatter
xdata = repmat(Temp, 5, 1);
ydata = [Bin1; Bin2; Bin3; Bin4; Bin5];
g = 2 * ones(size(ydata)); %everything is group 2
g(1:numel(Bin1)) = 1; %except initial data
gscatter(xdata, ydata, g)
In this particular case it is probably easier to handle things the way that @Arif Hoq suggested... but gscatter() is an option that sometimes makes a lot of sense to use.

Walter Roberson
Walter Roberson 2023 年 2 月 2 日
scatter() can take information about color in the 4th parameter. The color information can be set per-point.
xdata = repmat(Temp, 5, 1);
ydata = [Bin1; Bin2; Bin3; Bin4; Bin5];
g = 2 * ones(size(ydata)); %everything is group 2
g(1:numel(Bin1)) = 1; %except initial data
pointsize = [];
scatter(xdata, ydata, pointsize, g);
  1 件のコメント
Walter Roberson
Walter Roberson 2023 年 2 月 3 日
Current versions of scatter would also support
h = scatter(Temp, [Bin1, Bin2, Bin3, Bin4,Bin5]);
h(1).CData = [1 0 0]; %red, must be rgb in this context
set(h(2:end), 'CData', [0 1 0]); %green, must be rgb in this context
When you scatter() with multiple columns in a 2D array, each column becomes its own scatter() handle. Setting a property of a single handle such as h(1).CData can be done with assignment format, but setting the properties of multiple handles at the same time such as h(2:end) CData requires using set()

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

カテゴリ

Help Center および File ExchangeScatter Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by