目录
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.绘制无颜色的箱线图例子
文章来源:https://www.toymoban.com/news/detail-597259.html
当需要绘制无颜色的箱线图时,内部采用线条填充
但是:填充简单图例不好整,所以图例是再手动绘制的图例,需要慢慢调整文章来源地址https://www.toymoban.com/news/detail-597259.html
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', []);
到了这里,关于Matlab绘制箱线图的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!