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

조회 수: 7 (최근 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 CenterFile Exchange에서 Scatter Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by