How can I plot outliers in their correct position with respect to the original data?

12 views (last 30 days)
This plotting does not show the outliers in correct order. I want to plot the original data, and then mark the outliers in their correct order. The resulting graph is this. I want the red points in line with other data points. Any suggestions?
Temp = data(:,2); % Separating the feature vector
Temp_z_score = (Temp - mean(Temp)) / std(Temp); % Calculation of the z-score (z-score = the number of standard deviations a point is below or over the mean)
threshold = abs(Temp_z_score)>1.8; % Setting the outliers threshold
figure
plot(Temp(~threshold),'bo','DisplayName','Temperature')
hold on
plot(Temp(threshold),'r*','DisplayName','Outlier')
hold off
grid on

Accepted Answer

dpb
dpb on 15 Oct 2022
You're just plotting the selected array elements against their ordinal number; you need a corollary x value against which to plot them...one could assume that would be the first column in the data array...
z_score=zscore(data(:,2));
isOut=abs(z_score)>1.8;
figure
plot(data(~isOut,1),data(~isOut,2),'bo','DisplayName','Temperature')
hold on
plot(data(isOut,1),data(isOut,2),'r*','DisplayName','Outlier')
hold off
grid on
If the actual x values aren't the first column after all, then use
plot(find(~isOut),data(~isOut,2),'bo','DisplayName','Temperature')
hold on
plot(find(isOut),data(isOut,2),'r*','DisplayName','Outlier')
instead to return the locations in the original vector

More Answers (0)

Community Treasure Hunt

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

Start Hunting!