Add a specific line or point in boxplots
35 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi, i need help. Looked through a lot of community posts, but none helped.
I need to plot multiple boxes from which i know the lower and upper values. Futhermore within each box i need to plot a single value as point or straight line (showed in green). When i use boxplots it automatically calculates a mean value and plots it in red. I need to get rid of that and replace it with my desired green line (or as a point).
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1033485/image.png)
For example i saw this, where linestyle and color can be set. Can i also set the value of 'Median' ??
Thank you in advance!! :)
load carsmall;
boxplot(MPG)
h = findobj(gca,'tag','Median');
set(h,'linestyle','-.');
set(h,'Color',[0 0 0])
0 commentaires
Réponse acceptée
Voss
le 15 Juin 2022
Here's how you can set the value of those median lines.
In this example I'll change them from the median MPG to the mean MPG (you can make them whatever value you want):
load carsmall;
MPG = MPG+[0 10]; % make MPG have two columns, so we get a boxplot with multiple boxes
h = boxplot(MPG); % make the boxplot
idx = find(strcmp(get(h,'Tag'),'Median')); % find the median lines
get(h(idx),'YData') % the median lines' YData is the median values
MPG_mean = mean(MPG,1,'omitnan') % calculate the mean values (use whatever value you want)
for ii = 1:numel(idx) % put the median lines at the mean values
set(h(idx(ii)),'YData',MPG_mean(ii)*[1 1]);
end
get(h(idx),'YData') % check that the median lines' YData is now the mean values
set(h(idx), ... % change the Color and LineWidth too
'Color',[0 0.6 0], ...
'LineWidth',2);
2 commentaires
Voss
le 17 Juin 2022
Modifié(e) : Voss
le 17 Juin 2022
That popup is the datatip for the boxplot. To modify what it shows, you can define a custom function:
function txt = cb_update_boxplot_datatip(src,evt)
ch = reshape(get(get(src,'Parent'),'Children'),[],7);
ch = ch(end-evt.Position(1)+1,:);
tags = get(ch,'Tag');
y_min = get(ch(strcmp(tags,'Lower Adjacent Value')),'YData');
y_max = get(ch(strcmp(tags,'Upper Adjacent Value')),'YData');
y_middle = get(ch(strcmp(tags,'Median')),'YData');
txt = sprintf('Minimum: %.2f\nMaximum: %.2f\nMiddle: %.2f', ...
y_min(1),y_max(1),y_middle(1));
end
(That same function is attached in an m-file here; you can download it.)
And then on the command line, run this command to set the UpdateFcn of the datacursormode in the current figure (assumed to be the figure that contains your boxplot) to the function you just defined:
set(datacursormode(gcf()),'UpdateFcn',@cb_update_boxplot_datatip)
Now when you have a data cursor on in that figure, it will show the custom datatip.
I labeled the green-line value as 'Middle' in the function, since it's not necessarily the median anymore, but you can change that to whatever you want it to say, or change anything else about what shows up in the datatip now.
Plus de réponses (0)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!