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

9 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

Arif Hoq
Arif Hoq am 2 Feb. 2023
Bearbeitet: Arif Hoq am 2 Feb. 2023
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')

Weitere Antworten (2)

Walter Roberson
Walter Roberson am 2 Feb. 2023
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 am 2 Feb. 2023
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 Kommentar
Walter Roberson
Walter Roberson am 3 Feb. 2023
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()

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by