It is not currently easy to modify the appearance of boxplots. This function was made for personal use, but I thought it could be useful to a wider audience (e.g., https://au.mathworks.com/matlabcentral/answers/2047552-changing-colors-of-outlier-markers-median-and-whiskers-in-a-box-and-whisker-plot).
Function inputs:
- ax The axis of the boxplot you want to modify
- groupNumber Which groups on the plot you want to modify (integers). 'all' applies the changes to all groups on the figure
- variable inputs in triplets (see below)
The variable inputs are formatted as:
PlotElement, ElementProperty, PropertyValue
For example, if you wanted to make the median line blue, this would be: 'Median','Color','b'
if this was for group 2 on an axis ax, the input would be
BoxPlotFormat(ax,2, 'Median','Color','b')
.
List of PlotElement + ElementProperty
- Outliers MarkerSize, Marker, MarkerEdgeColor, MarkerFaceColor
- Median Color, LineStyle, LineWidth
- Box Color, LineStyle, LineWidth
- Whiskers Color, LineStyle, LineWidth
- WhiskerCaps Color, LineStyle, LineWidth, CapWidth (double)
I made a few changes from default boxplot figure groups.
- Instead of entering 'Upper Whisker' & 'Lower Whisker' seperately, you can just enter 'Whiskers'.
- Instead of entering 'Lower Adjacent Value' & 'Upper Adjacent Value' seperately, you can just enter 'WhiskerCaps'.
- 'CapWidth' was added to modify the width of WhiskerCaps
I can not guarantee that any elements or properties not listed here will work, but I have verified that it works for the above lists, which I believe are the most useful properties.
Here is some example code:
load carsmall
x1 = normrnd(5,1,100,1);
x2 = normrnd(6,1,100,1);
figure;
boxplot(MPG,Origin,'Symbol','.')
title('Miles per Gallon by Vehicle Origin (Default Box Plot Behaviour)')
xlabel('Country of Origin')
ylabel('Miles per Gallon (MPG)')
figure;
boxplot(MPG,Origin,'Symbol','.')
title('Miles per Gallon by Vehicle Origin (Modified with BoxPlotFormat)')
xlabel('Country of Origin')
ylabel('Miles per Gallon (MPG)')
BoxPlotFormat(gca,'all','Outliers','MarkerEdgeColor',[0.7,0.7,0.7],'Outliers','MarkerSize',16,...
'Whiskers','LineStyle','-',...
'WhiskerCaps','CapWidth',1.75,'WhiskerCaps','LineWidth',2,...
'Box','LineWidth',1.25,'Box','Color',[102 162 217]/255,...
'Median','Color',[238 79 79]/255,'Median','LineStyle','--')
BoxPlotFormat(gca,6,'WhiskerCaps','CapWidth',5)
BoxPlotFormat(gca,5,'Box','Color',[0.5,1,0.5],'Median','LineStyle',':')
BoxPlotFormat(gca,4,'Box','LineWidth',4,'Median','LineStyle','-')
BoxPlotFormat(gca,[2,3],'WhiskerCaps','LineWidth',4)
인용 양식
Robert Groth (2025). BoxPlotFormat (https://kr.mathworks.com/matlabcentral/fileexchange/171419-boxplotformat), MATLAB Central File Exchange. 검색 날짜: .
MATLAB 릴리스 호환 정보
개발 환경:
R2023b
모든 릴리스와 호환
플랫폼 호환성
Windows macOS Linux태그
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!| 버전 | 게시됨 | 릴리스 정보 | |
|---|---|---|---|
| 1.0.0 |