当前位置:   article > 正文

Matlab绘制箱线图_matlab画箱线图

matlab画箱线图

目录

1.箱线图例子1

 2.绘制无颜色的箱线图例子


1.箱线图例子1

  1. clc;clear all;close all;
  2. p=[1 2 3 4 5 6 7 8;2 3 4 5 6 7 8 9;3 4 5 6 7 8 9 10];
  3. t=[1 2 3 4 5 6 7 8;2 3 4 5 6 7 8 9;3 4 5 6 7 8 9 10];
  4. figure(1)
  5. position_O = 0.9:1:2.9;
  6. % Define position for 12 Month_O boxplots
  7. box_O = boxplot(p(1:3,:)','colors',[255,5,87]/255,'positions',position_O,'width',0.2,'Symbol','o', 'OutlierSize',4);
  8. % set(box_O,'LineWidth',1.5)
  9. hold on;
  10. set(gca,'XTickLabel',{'Jan' 'Feb' 'Mar' 'Apr' 'May' 'Jun' 'Jul' 'Aug' 'Sep'}); % Erase xlabels
  11. %hold on % Keep the Month_O boxplots on figure overlap the Month_S boxplots
  12. % Boxplot for the simulated temperature from January to December
  13. position_S = 1.2:1:3.2; % Define position for 12 Month_S boxplots
  14. box_S = boxplot(p(1:3,:)','colors',[0, 80, 255]/255,'positions',position_S,'width',0.2,'Symbol','o', 'OutlierSize',4);
  15. fillcolor2=[255,5,87]/255; %这是红色
  16. fillcolor1=[0, 80, 255]/255; % fillcolors = rand(24, 3);
  17. [t,z]=size(p);
  18. fillcolors=[repmat(fillcolor1,t,1);repmat(fillcolor2,t,1)];
  19. boxobj= findobj(gca,'Tag','Box');
  20. for j=1:length(boxobj) %给每个添加颜色
  21. hx=patch(get(boxobj(j),'XData'),get(boxobj(j),'YData'),fillcolors(j,:),'FaceAlpha',0.5);
  22. end
  23. ylabel('单位')
  24. set(gca,'ylim',[0 11]);
  25. set(gca,'XTickLabel',{"1月",'2月','3月'})
  26. set(gca,'XTickLabelRotation', 30);
  27. set(gca,'TickDir','out');
  28. boxchi = get(gca, 'Children');
  29. legend([boxchi(1),boxchi(5)], ["训练集", "测试集"] );
  30. box off
  31. ax2 = axes('Position',get(gca,'Position'),...
  32. 'Color','none',...
  33. 'XAxisLocation','top',...
  34. 'YAxisLocation','right',...
  35. 'XColor','k','YColor','k');
  36. set(ax2,'YTick', []);
  37. set(ax2,'XTick', []);

 2.绘制无颜色的箱线图例子

 当需要绘制无颜色的箱线图时,内部采用线条填充
但是:填充简单图例不好整,所以图例是再手动绘制的图例,需要慢慢调整

  1. clc;clear all;close all;
  2. p=[0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8;0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9;0.3 0.4 0.5 0.6 0.7 0.8 0.9 0.10];
  3. p=[0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8;0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9;0.3 0.4 0.5 0.6 0.7 0.8 0.9 0.10];
  4. figure(1)
  5. position_O = 0.9:1:2.9;
  6. % Define position for 12 Month_O boxplots
  7. box_O = boxplot(p(1:3,:)','colors',[0,0,0],'MedianStyle','target','positions',position_O,'width',0.2,'Symbol','o', 'OutlierSize',4);
  8. % set(box_O,'LineWidth',1.5)
  9. hold on;
  10. set(gca,'XTickLabel',{'Jan' 'Feb' 'Mar' 'Apr' 'May' 'Jun' 'Jul' 'Aug' 'Sep'}); % Erase xlabels
  11. %hold on % Keep the Month_O boxplots on figure overlap the Month_S boxplots
  12. % Boxplot for the simulated temperature from January to December
  13. position_S = 1.2:1:3.2; % Define position for 12 Month_S boxplots
  14. box_S = boxplot(p(1:3,:)','colors',[0, 0, 0],'MedianStyle','target','positions',position_S,'width',0.2,'Symbol','o', 'OutlierSize',4);
  15. fillcolor2=[255,5,87]/255; %这是红色
  16. fillcolor1=[0, 80, 255]/255; % fillcolors = rand(24, 3);
  17. [t,z]=size(p);
  18. fillcolors=[repmat(fillcolor1,t,1);repmat(fillcolor2,t,1)];
  19. boxobj= findobj(gca,'Tag','Box');
  20. for j=1:3 %给每个添加颜色和图案
  21. hx=patch(get(boxobj(j),'XData'),get(boxobj(j),'YData'),fillcolors(j,:),'FaceAlpha',0);
  22. hatchfill2(hx,'single','HatchAngle',0,'LineWidth',0.1,'HatchLineWidth',1,'HatchDensity',60);
  23. end
  24. for j=4:6 %给每个添加颜色和图案
  25. hx=patch(get(boxobj(j),'XData'),get(boxobj(j),'YData'),fillcolors(j,:),'FaceAlpha',0);
  26. hatchfill2(hx,'cross','LineWidth',0.1,'HatchLineWidth',1,'HatchDensity',60);
  27. end
  28. ylabel('单位')
  29. set(gca,'ylim',[0 1.2]);
  30. set(gca,'XTickLabel',{"1月",'2月','3月'})
  31. set(gca,'XTickLabelRotation', 30);
  32. set(gca,'TickDir','out');
  33. % boxchi = get(gca, 'Children');
  34. % legend([boxchi(2),boxchi(4)], ["训练集", "测试集"] );
  35. % 绘制图例
  36. x2 = [2.75 3 3 2.75];
  37. y2 = [1.07 1.07 1.12 1.12];
  38. hx=patch(x2,y2,[1 1 1],'FaceAlpha',0);
  39. hatchfill2(hx,'cross','LineWidth',0.1,'FaceColor','k','HatchLineWidth',1,'HatchDensity',60);
  40. text(3,1.025,'测试集'); %这个是根据坐标轴的位置
  41. x = [2.75 3 3 2.75];
  42. y = [1 1 1.05 1.05];
  43. hx=patch(x,y,[1 1 1],'FaceAlpha',0);
  44. hatchfill2(hx,'single','HatchAngle',0,'LineWidth',0.1,'FaceColor','k','HatchLineWidth',1,'HatchDensity',85);
  45. text(3,1.09,'训练集'); %这个是根据坐标轴的位置
  46. % 绘制图例的方框
  47. hold on
  48. plot([2.7 3.3],[0.95 0.95],'k'); %下线
  49. plot([2.7 3.3],[1.17 1.17],'k'); %上线
  50. plot([2.7 2.7],[0.95 1.17],'k'); %左线
  51. plot([3.3 3.3],[0.95 1.17],'k'); %右线
  52. box off
  53. ax2 = axes('Position',get(gca,'Position'),...
  54. 'Color','none',...
  55. 'XAxisLocation','top',...
  56. 'YAxisLocation','right',...
  57. 'XColor','k','YColor','k');
  58. set(ax2,'YTick', []);
  59. set(ax2,'XTick', []);

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/303633
推荐阅读
相关标签
  

闽ICP备14008679号