How do I change the maximum outlier marker style?

조회 수: 5 (최근 30일)
Radha Reddy
Radha Reddy 2022년 10월 12일
답변: Meet 2024년 9월 18일
Hello,
I am trying to change the marker style of the maximum outlier to different than the other outliers in the boxplot. Is it possible to change only one outlier style?
Many thanks in advance.
Radha

답변 (1개)

Meet
Meet 2024년 9월 18일
Hi Radha,
To change the color of the maximum outlier in a boxplot, you may follow these steps:
  1. Use MATLAB's "boxplot" function to visualize your data. This will automatically identify and display any outliers in the dataset.
  2. Retrieve the graphical objects representing outliers using the "findobj" function with the tag 'Outliers'. This will allow you to access their properties.
  3. Obtain the y-coordinates 'YData' of the outliers. If the data is returned as a cell array, convert it to a numeric array for easier manipulation.
  4. Use the “max” function to identify the outlier with the highest value. This step will provide both the value and index of this maximum outlier.
  5. Overlay a new marker on the maximum outlier using the “plot” function. Customize this marker to distinguish it from others, such as changing its color and size.
Refer to the code provided below for a detailed implementation:
% Sample data
data = randn(100, 1);
data(end+1) = 5; % Add an outlier
% Create the boxplot
h = boxplot(data);
% Get the outliers
outliers = findobj(h, 'Tag', 'Outliers');
% Extract the YData of the outliers
outlierYData = get(outliers, 'YData');
% If there are multiple outliers, outlierYData will be a cell array
if iscell(outlierYData)
outlierYData = cell2mat(outlierYData);
end
% Find the maximum outlier
[maxOutlierValue, maxOutlierIndex] = max(outlierYData);
% Extract the XData corresponding to the maximum outlier
outlierXData = get(outliers, 'XData');
if iscell(outlierXData)
outlierXData = cell2mat(outlierXData);
end
% Set new marker properties for the maximum outlier
hold on;
% Overlay the maximum outlier with a different color
plot(outlierXData(maxOutlierIndex), maxOutlierValue, 'ko', 'MarkerSize', 8, 'LineWidth', 2);
hold off;
You can refer to the documentation links below for more information:

Community Treasure Hunt

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

Start Hunting!

Translated by