Control Whiskers on Boxchart or add legend to Boxplot

21 visualizaciones (últimos 30 días)
Martin Danz
Martin Danz el 25 de Mayo de 2022
Respondida: Jaynik el 27 de Sept. de 2023
Hi,
I'm trying to create a boxplot where the whiskers extend to the actual max and min. This is possible using boxplot, not with boxchart.
At the same time, I want to use the colored grouping and add a legend for the colors. This is possible using boxchart, not with boxplot.
Is there a solution I do not see?

Respuestas (1)

Jaynik
Jaynik el 27 de Sept. de 2023
Hi Martin,
I understand that you are trying to extend whiskers in the boxchart and add legend for colors in the boxplot and are facing issues with it. There is no direct way possible to perform both tasks. As for adding a colored legend, you can search for the box object and add the legend entry for each box.
data = [randn(100,1) rand(100, 1) rand(100, 1)]; % Initialize a random data
figure;
boxplot(data, 'Colors', 'rbg', 'Labels', {'Group 1', 'Group 2', 'Group 3'});
h = findobj(gca, 'Tag', 'Box');
legendEntries = cell(1, size(data, 2)); % Create a cell array for legend entries
for i = 1:size(data, 2)
legendEntries{i} = ['Variable ' num2str(i)];
end
legend(h, legendEntries);
For grouping data for boxplot, you will need to perform manual adjustments in the code. Also, boxplot only accepts numeric data; for data in table format, you will need to use boxchart.
Hope this helps!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by