Increasing the line width of box plot whiskers
Show older comments
I have written some code to produce 2 boxplots side by side using tiledlayout. I would like to increase the line weight of the boxes and the whiskers and have written the code to do so as follows:
figure()
t = tiledlayout(1,2,'tilespacing','none');
nexttile
boxplot(T_toPlot,'BoxStyle','outline','Color', 'b', 'Symbol','xk', 'OutlierSize',8,'Labels', labels);
bx1 = findobj('Tag','boxplot');
set(bx1.Children,'LineWidth',2)
h = findobj(gca,'Tag','Box');
for j=1:length(h)
patch(get(h(j),'XData'),get(h(j),'YData'),get(h(j),'Color'),'FaceAlpha',.5);
end
clear h j
nexttile
boxplot(S_toPlot,'BoxStyle','outline','Color', 'b', 'Symbol','xk', 'OutlierSize',8,'Labels', labels);
bx2 = findobj('Tag','boxplot');
set(bx2.Children,'LineWidth',2)
h = findobj(gca,'Tag','Box');
for j=1:length(h)
patch(get(h(j),'XData'),get(h(j),'YData'),get(h(j),'Color'),'FaceAlpha',.5);
end
clear h j
The first time I run this code, the first tile runs successfully and I get the following error message for the second tile:
Error using matlab.graphics.primitive.Line/set
Invalid parameter/value pair arguments.
Error in Filename (line XXX)
set(bx2.Children,'LineWidth',2)
If I then do nothing, just hit enter again to re-run the cell, it then fails with the same error, but now for the first tile - and does not reach the second tile. Even if I clear the workspace and re-run the data processing from scratch, subsequent re-runs will fail for the first tile unless I restart the program. How can I increase the line weight of the boxplots (particularly the whiskers) without running into this problem? Thanks!
Accepted Answer
More Answers (2)
M1 = rand(10,2);
M2 = rand(10,2) + 0.5;
labels = {'A','B'};
t=tiledlayout(1,2,"TileSpacing","none");
ax1 = nexttile();
b1 =boxchart(ax1,M1,SeriesIndex=1,...
LineWidth=1.5,WhiskerLineStyle='--');
ax1.XTickLabel = labels;
ax2 = nexttile;
b2 = boxchart(ax2,M2,SeriesIndex=1);
ax2.XTickLabel = labels;
% We can also easily modify all kinds of properties afterwards:
b2.WhiskerLineStyle = '--';
b2.LineWidth = b1.LineWidth;
% Let's synchronize the y-axes for good measure:
linkaxes([ax1, ax2],'y')
Alternatively, we could set the properties on b2 in one go using the set function:
set(b2,"LineWidth",2,"WhiskerLineStyle",'--')
dpb
on 29 Aug 2024
...
hBx=findobj('Tag','boxplot'); % the boxplot group handle
hBx=find(hBx.Children,'-not','Tag','Outliers'); % the group lines less outliers
set(hBx,'LineWidth',2)
...
should work ok; the problem is the outliers are also line objects which may be different sizes. The above returns the handles only for the other lines. If you also want the outlier markers heavier, you need to retrieve those handles separately and either loop or use the expanded version of set() with cell arguments to handle the different data by handle.
Without your specific data we can't duplicate exact code here.
I'd recommend considering the alternate boxchart if you want to modify things; it exposes most of the properties without the grief of having to search for them. Why on earth Mathworks thought hiding stuff inside boxplot was a good idea simply boggles the mind, though...although that goes for all the specialized plots; just some are more egregious than others.
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
