MATLAB Answers

Is it possible to add an 'other' bar to a PARETO plot in MATLAB 7.8 (R2009a)?

1 view (last 30 days)
Currently PARETO will always plot data in descending order as shown in the following plot:
codelines = [200 120 555 608 1024 101 57 687,5000];
coders = ...
{'Fred','Ginger','Norman','Max','Julia','Wally','Heidi','Pat','Other'};
H = pareto(codelines, coders)
title('Lines of Code by Programmer')
I would like to create a PARETO plot with an 'other' category so that even if 'other' is greater than all other categories, it will be plotted last rather than first.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
The ability to use PARETO with an 'other' category is not possible in MATLAB 7.8 (R2009a).
As a workaround, you may be able to alter the code for the PARETO function. Please open pareto.m by typing 'edit pareto.m' at the Command Prompt. Then, make the following changes at the given locations:
On Lines 51-57:
for p=1:size(names,1)
if strcmp(names(size(names,1)+1-p,1:5),'Other')
% names=[names(1:p-1,:);names(p+1:size(names,1),:);names(p,:)];
yy=[yy(1:p-1);yy(p+1:length(yy));yy(p)];
ndx=[ndx(1:p-1);ndx(p+1:size(ndx,1));ndx(p)];
end
end
The above takes care of making sure the 'Other' category will come last.
On Lines 76-79:
% k = min(find(cumsum(yy)/ysum>.95,1),10);
%
% if isempty(k), k = min(length(y),10); end
k=length(y);
The above code takes care of the tail end. If we cut on 95% variability the 'Other' categories will not be included in the chart.

More Answers (1)

Pamudu Ranasinghe
Pamudu Ranasinghe on 6 Jun 2021
function MyParetoDig(X,data)
%Pamudu Ranasinghe
%2021/06/06
%This Function takes two Parmaters "X" and "data"
%For Example
%X=[5 6 0 2 1];
%data=["Pamudu","Pasindu","rana","oshi"];
while true
finished = 1;
for i=1:size(X,2)-1
if X(i)>X(i+1)
temp=X(i);
tempData=data(i);
X(i)=X(i+1);
data(i)=data(i+1);
X(i+1)=temp;
data(i+1)=tempData;
finished = 0;
end
end
if finished
break
end
end
values=flip(X);
data=flip(data);
for k=1:length(data)
if data(k)=="other"
otherIndex=k;
end
end
valuesNew = [values([1:otherIndex-1, otherIndex+1:end]),values(otherIndex)];
dataNew = [data([1:otherIndex-1, otherIndex+1:end]),data(otherIndex)];
%%Plot the Bar chart
x=categorical(dataNew);
y=valuesNew;
bar(y)
set(gca,'xticklabel',x)
%%Get the cummalative Values
cumm=[];
tot=0;
for c=1:length(valuesNew)
tot=tot+valuesNew(c);
cumm(c)=tot;
end
hold on
plot(cumm,'LineWidth',2)
end

Tags

No tags entered yet.

Products


Release

R2009a

Community Treasure Hunt

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

Start Hunting!

Translated by