Adding a scatter of points to a boxplot

조회 수: 92 (최근 30일)
Peyman Obeidy
Peyman Obeidy 2018년 4월 29일
편집: Seth DeLand 2022년 5월 26일
Does anyone come with with a code which can match the python generated boxplot?
  댓글 수: 1
dpb
dpb 2018년 4월 29일
Presuming the points actually a set of coordinates as shown, don't see why
hold on
scatter(x,y)
with appropriate x,y arrays and the associated color arrays, etc., wouldn't come reasonably close...

댓글을 달려면 로그인하십시오.

채택된 답변

dpb
dpb 2018년 4월 29일
Indeed, that seems to work just fine...
load carsmall MPG % the sample dataset variable
hold on
scatter(ones(size(MPG)).*(1+(rand(size(MPG))-0.5)/10),MPG,'r','filled')
yields
It's possible to add color with value scaling in scatter see the details on it for all the particulars.
  댓글 수: 5
Rubina Chandnani
Rubina Chandnani 2021년 7월 22일
In the line of code in scatter, is there a way to use a different color using uisetcolor? (I don't want to use the default colors).
Seth DeLand
Seth DeLand 2022년 5월 26일
편집: Seth DeLand 2022년 5월 26일
I'd like to add that there is now an easier way to do this with boxchart (added in R2020a) and swarmchart (added in R2020b):
load carsmall MPG % the sample dataset variable
MPG(:,2)=MPG(:,1).*2;
MPG(:,3)=MPG(:,1).*3;
boxchart(MPG)
hold on
x = repmat(1:3,100,1); % create the x data needed to overlay the swarmchart on the boxchart
swarmchart(x,MPG,[],'red')

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Hassan
Hassan 2019년 5월 18일
Hi, here you can find a one line solution for the jitter like function using the 'undocumented matlab' features.
load carsmall MPG
figure;
MPG(:,2)=MPG(:,1).*2;
MPG(:,3)=MPG(:,1).*3;
boxplot(MPG);
hold on;
x=repmat(1:3,length(MPG),1);
scatter(x(:),MPG(:),'filled','MarkerFaceAlpha',0.6','jitter','on','jitterAmount',0.15);
Best, HM
  댓글 수: 1
Junru Ruan
Junru Ruan 2019년 12월 13일
This is the best answer! very nice presentation.
Tips: if you used group in box plot, do a 'unique' to get the right x axis.
boxplot(report_table.data,report_table.group_id);
hold on
[C, ~, ic]= unique([report_table.group_id],'stable');
scatter(ic,report_table.data,'filled','MarkerFaceAlpha',0.6','jitter','on','jitterAmount',0.15);
xlabel('Group ID');
ylabel('Data');
hold off

댓글을 달려면 로그인하십시오.


Ernesto Salcedo
Ernesto Salcedo 2020년 11월 27일
편집: Ernesto Salcedo 2020년 11월 27일
Boxchart solution for grouped categorical data

Table with random group

count = 20;
T = table(randi(10,count,1), categorical(repmat(["papaya";"silicon"], count/2,1)),'VariableNames',["Recharges","model"])
T.idx = grp2idx(T.model); % convert categories into group indices

Boxchart

figure
hc = boxchart(T.idx, T.Recharges); % group by index
hold on
% overlay the scatter plots
for n=1:max(unique(T.idx))
hs = scatter(ones(sum(T.idx==n),1) + n-1, T.Recharges(T.idx == n),"filled",'jitter','on','JitterAmount',0.1);
hs.MarkerFaceAlpha = 0.5;
end
set(gca,"XTick", unique(T.idx),"XTickLabel",categories(T.model))

카테고리

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