testviolinplot.m 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. % TEST CASE 1
  2. disp('Test 1: Violin plot default options');
  3. load carbig MPG Origin
  4. Origin = cellstr(Origin);
  5. figure
  6. vs = violinplot(MPG, Origin);
  7. ylabel('Fuel Economy in MPG');
  8. xlim([0.5, 7.5]);
  9. disp('Test 1 passed ok');
  10. % TEST CASE 2
  11. disp('Test 2: Test the plot ordering option');
  12. grouporder={'USA','Sweden','Japan','Italy','Germany','France','England'};
  13. figure;
  14. vs2 = violinplot(MPG,Origin,'GroupOrder',grouporder);
  15. disp('Test 2 passed ok');
  16. xlim([0.5, 7.5]);
  17. % TEST CASE 3
  18. disp('Test 3: Test the numeric input construction mode');
  19. figure
  20. cats = categorical(Origin);
  21. catnames = (unique(cats)); % this ignores categories without any data
  22. catnames_labels = {};
  23. thisData = NaN(length(MPG),length(catnames));
  24. for n = 1:length(catnames)
  25. thisCat = catnames(n);
  26. catnames_labels{n} = char(thisCat);
  27. thisData(1:length(MPG(cats == thisCat)),n) = MPG(cats == thisCat);
  28. end
  29. vs3 = violinplot(thisData,catnames_labels);
  30. xlim([0.5, 7.5]);
  31. disp('Test 3 passed ok');
  32. % TEST CASE 4
  33. disp('Test 4: Test two sided violin plots. Japan is being compared.');
  34. figure
  35. C = colororder;
  36. vs4 = violinplot({thisData,repmat(thisData(:,5),1,7)},catnames_labels,'ViolinColor',{C,C(5,:)},'ViolinAlpha',{0.3 0.3});
  37. xlim([0.5, 7.5]);
  38. disp('Test 4 passed ok');
  39. %other test cases could be added here