358 views (last 30 days)
Show older comments
Peyman Obeidy on 29 Apr 2018
Edited: Seth DeLand on 26 May 2022
Accepted Answer: dpb
- boxplot_inpy.png
Does anyone come with with a code which can match the python generated boxplot?
1 Comment Show -1 older commentsHide -1 older comments
Show -1 older commentsHide -1 older comments
dpb on 29 Apr 2018
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/398012-adding-a-scatter-of-points-to-a-boxplot#comment_562297
Open in MATLAB Online
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...
Sign in to comment.
Sign in to answer this question.
Accepted Answer
dpb on 29 Apr 2018
Open in MATLAB Online
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 Comments Show 3 older commentsHide 3 older comments
Show 3 older commentsHide 3 older comments
Peyman Obeidy on 30 Apr 2018
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/398012-adding-a-scatter-of-points-to-a-boxplot#comment_562422
Open in MATLAB Online
Here is the same thing with a bit more manipulations
load carsmall MPG % the sample dataset variable
MPG(:,2)=MPG(:,1).*2;
MPG(:,3)=MPG(:,1).*3;
boxplot(MPG,'Notch','on','Labels',{'mu = 5','mu = 6','mu = 6'},'Whisker',1)
lines = findobj(gcf, 'type', 'line', 'Tag', 'Median');
set(lines, 'Color', 'g');
% Change the boxplot color from blue to green
a = get(get(gca,'children'),'children'); % Get the handles of all the objects
%t = get(a,'tag'); % List the names of all the objects
%box1 = a(7); % The 7th object is the first box
set(a, 'Color', 'r'); % Set the color of the first box to green
hold on
x=ones(length(MPG)).*(1+(rand(length(MPG))-0.5)/5);
x1=ones(length(MPG)).*(1+(rand(length(MPG))-0.5)/10);
x2=ones(length(MPG)).*(1+(rand(length(MPG))-0.5)/15);
f1=scatter(x(:,1),MPG(:,1),'k','filled');f1.MarkerFaceAlpha = 0.4;hold on
f2=scatter(x1(:,2).*2,MPG(:,2),'k','filled');f2.MarkerFaceAlpha = f1.MarkerFaceAlpha;hold on
f3=scatter(x2(:,3).*3,MPG(:,3),'k','filled');f3.MarkerFaceAlpha = f1.MarkerFaceAlpha;hold on
dpb on 30 Apr 2018
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/398012-adding-a-scatter-of-points-to-a-boxplot#comment_562430
Nice choices..looks good. :)
Peyman Obeidy on 30 Apr 2018
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/398012-adding-a-scatter-of-points-to-a-boxplot#comment_562432
Thank you
Rubina Chandnani on 22 Jul 2021
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/398012-adding-a-scatter-of-points-to-a-boxplot#comment_1650424
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 on 26 May 2022
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/398012-adding-a-scatter-of-points-to-a-boxplot#comment_2180020
Edited: Seth DeLand on 26 May 2022
Open in MATLAB Online
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')
Sign in to comment.
More Answers (2)
Hassan on 18 May 2019
Open in MATLAB Online
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 Comment Show -1 older commentsHide -1 older comments
Show -1 older commentsHide -1 older comments
Junru Ruan on 13 Dec 2019
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/398012-adding-a-scatter-of-points-to-a-boxplot#comment_777810
Open in MATLAB Online
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
Sign in to comment.
Ernesto Salcedo on 27 Nov 2020
Edited: Ernesto Salcedo on 27 Nov 2020
Open in MATLAB Online
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))
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
See Also
Categories
MATLABGraphics2-D and 3-D PlotsData Distribution PlotsScatter Plots
Find more on Scatter Plots in Help Center and File Exchange
Tags
- scatter
- boxplot
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office