Skip to content

Commit

Permalink
Fix regression caused by unique.
Browse files Browse the repository at this point in the history
- Now the calcKernelDensity function returns the mean value for "value" if min and max from data are equal. Which was likely the cause in the errors for interp1.
- To test this case, fake data for MPG is added in the testviolinplot.m file.

	modified:   test_cases/testviolinplot.m

	modified:   Violin.m
	modified:   test_cases/testviolinplot.m
  • Loading branch information
mikelgg93 committed Jun 8, 2022
1 parent e0955bc commit 2f14918
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
5 changes: 3 additions & 2 deletions Violin.m
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@

%% Plot the data points within the violin area
if length(density) > 1
jitterstrength = interp1(unique(value), unique(density*width), data);
jitterstrength = interp1(value, density*width, data, 'linear','extrap');
else % all data is identical:
jitterstrength = density*width;
end
Expand Down Expand Up @@ -314,7 +314,7 @@
%% Plot the data mean
meanValue = mean(data);
if length(density) > 1
meanDensityWidth = interp1(unique(value), unique(density), meanValue)*width;
meanDensityWidth = interp1(value, density, meanValue,'linear','extrap')*width;
else % all data is identical:
meanDensityWidth = density*width;
end
Expand Down Expand Up @@ -695,6 +695,7 @@
% all data is identical
if min(data) == max(data)
density = 1;
value= mean(value);
end

width = width/max(density);
Expand Down
8 changes: 5 additions & 3 deletions test_cases/testviolinplot.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function testviolinplot()
disp('Test 4: Test two sided violin plots. Japan is being compared.');
subplot(2,4,4);
C = colororder;
vs4 = violinplot({thisData,repmat(thisData(:,5),1,7)},catnames_labels,'ViolinColor',{C,C(5,:)},'ViolinAlpha',{0.3 0.3});
vs4 = violinplot({thisData,repmat(thisData(:,5),1,7)},catnames_labels,'ViolinColor',{C,C(5,:)},'ViolinAlpha',{0.3 0.3}, 'ShowMean', true);
plotdetails(4);

% TEST CASE 5
Expand All @@ -65,13 +65,15 @@ function testviolinplot()
% TEST CASE 8
disp('Test 8: Same as previous one, just removing the data of half of the violins afterwards.');
subplot(2,4,8);
vs5 = violinplot(MPG, Origin, 'QuartileStyle','shadow', 'HalfViolin','left',...
'DataStyle', 'histogram', 'ShowMean', false);
vs5 = violinplot([MPG; 5;5;5;5;5], [Origin; 'test';'test';'test';'test';'test'], 'QuartileStyle','shadow', 'HalfViolin','full',...
'DataStyle', 'scatter', 'ShowMean', false);
plotdetails(8);
for n= 1:round(length(vs5)/2)
vs5(1,n).ShowData = 0;
end
xlim([0, 9]);
%other test cases could be added here

end

function plotdetails(n)
Expand Down

0 comments on commit 2f14918

Please sign in to comment.