Matlab绘制箱线图

目录

1.箱线图例子1

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


1.箱线图例子1

clc;clear all;close all;

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];
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];
figure(1)
position_O = 0.9:1:2.9;
% Define position for 12 Month_O boxplots
box_O = boxplot(p(1:3,:)','colors',[255,5,87]/255,'positions',position_O,'width',0.2,'Symbol','o', 'OutlierSize',4);

% set(box_O,'LineWidth',1.5)
hold on;
set(gca,'XTickLabel',{'Jan' 'Feb' 'Mar' 'Apr' 'May' 'Jun' 'Jul' 'Aug' 'Sep'}); % Erase xlabels
%hold on % Keep the Month_O boxplots on figure overlap the Month_S boxplots
% Boxplot for the simulated temperature from January to December
position_S = 1.2:1:3.2; % Define position for 12 Month_S boxplots
box_S = boxplot(p(1:3,:)','colors',[0, 80, 255]/255,'positions',position_S,'width',0.2,'Symbol','o', 'OutlierSize',4);

fillcolor2=[255,5,87]/255;  %这是红色
fillcolor1=[0, 80, 255]/255; % fillcolors = rand(24, 3);
[t,z]=size(p);
fillcolors=[repmat(fillcolor1,t,1);repmat(fillcolor2,t,1)];
boxobj= findobj(gca,'Tag','Box');

for j=1:length(boxobj)   %给每个添加颜色
    hx=patch(get(boxobj(j),'XData'),get(boxobj(j),'YData'),fillcolors(j,:),'FaceAlpha',0.5);
end

ylabel('单位')
set(gca,'ylim',[0 11]);
set(gca,'XTickLabel',{"1月",'2月','3月'})
set(gca,'XTickLabelRotation', 30);
set(gca,'TickDir','out');
boxchi = get(gca, 'Children');
legend([boxchi(1),boxchi(5)], ["训练集", "测试集"] );

box off
ax2 = axes('Position',get(gca,'Position'),...
    'Color','none',...
    'XAxisLocation','top',...
    'YAxisLocation','right',...
    'XColor','k','YColor','k');
set(ax2,'YTick', []);
set(ax2,'XTick', []);

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

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

clc;clear all;close all;
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];
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];


figure(1)
position_O = 0.9:1:2.9;
% Define position for 12 Month_O boxplots
box_O = boxplot(p(1:3,:)','colors',[0,0,0],'MedianStyle','target','positions',position_O,'width',0.2,'Symbol','o', 'OutlierSize',4);

% set(box_O,'LineWidth',1.5)
hold on;
set(gca,'XTickLabel',{'Jan' 'Feb' 'Mar' 'Apr' 'May' 'Jun' 'Jul' 'Aug' 'Sep'}); % Erase xlabels
%hold on % Keep the Month_O boxplots on figure overlap the Month_S boxplots
% Boxplot for the simulated temperature from January to December
position_S = 1.2:1:3.2; % Define position for 12 Month_S boxplots
box_S = boxplot(p(1:3,:)','colors',[0, 0, 0],'MedianStyle','target','positions',position_S,'width',0.2,'Symbol','o', 'OutlierSize',4);

fillcolor2=[255,5,87]/255;  %这是红色
fillcolor1=[0, 80, 255]/255; % fillcolors = rand(24, 3);
[t,z]=size(p);
fillcolors=[repmat(fillcolor1,t,1);repmat(fillcolor2,t,1)];
boxobj= findobj(gca,'Tag','Box');

for j=1:3   %给每个添加颜色和图案

        hx=patch(get(boxobj(j),'XData'),get(boxobj(j),'YData'),fillcolors(j,:),'FaceAlpha',0);
        hatchfill2(hx,'single','HatchAngle',0,'LineWidth',0.1,'HatchLineWidth',1,'HatchDensity',60);
end
for j=4:6  %给每个添加颜色和图案
        hx=patch(get(boxobj(j),'XData'),get(boxobj(j),'YData'),fillcolors(j,:),'FaceAlpha',0);
        hatchfill2(hx,'cross','LineWidth',0.1,'HatchLineWidth',1,'HatchDensity',60);
end

ylabel('单位')
set(gca,'ylim',[0 1.2]);
set(gca,'XTickLabel',{"1月",'2月','3月'})
set(gca,'XTickLabelRotation', 30);
set(gca,'TickDir','out');
% boxchi = get(gca, 'Children');
% legend([boxchi(2),boxchi(4)], ["训练集", "测试集"] );

% 绘制图例
x2 = [2.75 3 3 2.75];
y2 = [1.07 1.07 1.12 1.12];
hx=patch(x2,y2,[1 1 1],'FaceAlpha',0);
hatchfill2(hx,'cross','LineWidth',0.1,'FaceColor','k','HatchLineWidth',1,'HatchDensity',60);
text(3,1.025,'测试集');                   %这个是根据坐标轴的位置

x = [2.75 3 3 2.75];
y = [1 1 1.05 1.05];
hx=patch(x,y,[1 1 1],'FaceAlpha',0);
hatchfill2(hx,'single','HatchAngle',0,'LineWidth',0.1,'FaceColor','k','HatchLineWidth',1,'HatchDensity',85);
text(3,1.09,'训练集');                   %这个是根据坐标轴的位置

% 绘制图例的方框
hold on
plot([2.7  3.3],[0.95 0.95],'k');  %下线
plot([2.7  3.3],[1.17 1.17],'k');  %上线
plot([2.7  2.7],[0.95 1.17],'k');  %左线
plot([3.3  3.3],[0.95 1.17],'k');  %右线

box off
ax2 = axes('Position',get(gca,'Position'),...
    'Color','none',...
    'XAxisLocation','top',...
    'YAxisLocation','right',...
    'XColor','k','YColor','k');
set(ax2,'YTick', []);
set(ax2,'XTick', []);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值