【MATLAB基础绘图第20棒】云雨图(Raincloud plots)

云雨图(Raincloud plots)

云雨图(Raincloud plots)其实是可以看成核密度估计曲线图、箱形图和抖动散点图的组合图,清晰、完整、美观地展示了所有数据信息。
本质上是一个混合图,可同时将原始数据、数据分布和关键汇总统计表现出来,由对分的小提琴图(Violin plot)、箱线图(boxplot)和作为某种散点的原始数据组成。

MATLAB绘制云雨图

横向云雨图

成图如下:
在这里插入图片描述
MATLAB代码如下:

clc
close all
clear
%% 函数说明-绘制云雨图

pathFigure= '.\Figures\' ;
figureUnits = 'centimeters';
figureWidth = 20; 
figureHeight = 10;

% 导入数据
X1=[normrnd(8,4,1,120),normrnd(5,2,1,25)];
X2=[normrnd(2.5,3,1,75),normrnd(6,4,1,25),normrnd(15,1,1,100)];
X3=[normrnd(4,3,1,40),normrnd(3,4,1,25)];
X4=[normrnd(4,3,1,40),normrnd(2,4,1,75)];

dataCell={X1,X2,X3,X4};           % 把数据放到元胞数组,要是数据太多可写循环放入  
dataName={'A','B','C','D'};        % 各个数据类的名称,可空着

%% 绘制横向云雨图

rainCloudsH(dataCell, dataName)

str= strcat(pathFigure, "图1 "+"横向云雨图", '.tiff');
print(gcf, '-dtiff', '-r600', str);
%% 调用函数

function rainCloudsH(dataCell, dataName)

% 颜色列表
colorList=[0.9294    0.7569    0.5059
           0.9176    0.5569    0.4627
           0.7020    0.4784    0.5451
           0.4863    0.4314    0.5490];      
% =========================================================================

classNum=length(dataCell);
if size(colorList,1)==0
    colorList=repmat([130,170,172]./255,[classNum,1]);
else
    colorList=repmat(colorList,[ceil(classNum/size(colorList,1)),1]);
end
if isempty(dataName)
    for i=1:classNum
        dataName{i}=['class',num2str(i)];
    end
end

figure(1)
% 坐标区域修饰
hold on; box on;
ax=gca;
ax.YLim=[1/2,classNum+2/3];
ax.YTick=1:classNum;
ax.LineWidth=1.2;
ax.YTickLabels=dataName(end:-1:1);
ax.FontSize=14;

rate=3.5;

% 绘制雨云图
for i=1:classNum
    tX=dataCell{i};tX=tX(:);
    [F,Xi]=ksdensity(tX);

    % 绘制山脊图
    patchCell(i)=fill([Xi(1),Xi,Xi(end)],0.2+[0,F,0].*rate+(classNum+1-i).*ones(1,length(F)+2),...
        colorList(i,:),'EdgeColor',[0,0,0],'FaceAlpha',0.8,'LineWidth',1.2);

    % 其他数据获取
    qt25=quantile(tX,0.25); % 下四分位数
    qt75=quantile(tX,0.75); % 上四分位数
    med=median(tX);         % 中位数

    outliBool=isoutlier(tX,'quartiles');  % 离群值点
    nX=tX(~outliBool);                    % 95%置信内的数
    
    % 绘制箱线图
    plot([min(nX),max(nX)],[(classNum+1-i),(classNum+1-i)],'k','lineWidth',1.2);
    fill([qt25,qt25,qt75,qt75],(classNum+1-i)+[-1 1 1 -1].*0.12,colorList(i,:),'EdgeColor',[0 0 0]);
    plot([med,med],[(classNum+1-i)-0.12,(classNum+1-i)+0.12],'Color',[0,0,0],'LineWidth',2.5)

    % 绘制散点 
    tY=(rand(length(tX),1)-0.5).*0.24+ones(length(tX),1).*(classNum+1-i);
    scatter(tX,tY,15,'CData',colorList(i,:),'MarkerEdgeAlpha',0.15,...
        'MarkerFaceColor',colorList(i,:),'MarkerFaceAlpha',0.1)
end

set(gca,'FontName','Times New Roman','FontSize',14, 'Layer','top','LineWidth',1);

% 绘制图例
hl = legend(patchCell,dataName);
set(hl,'Box','off','location','northOutside','NumColumns',4,'FontSize',16,'FontName','Times New Roman');    

end

竖向云雨图

成图如下:
在这里插入图片描述

MATLAB代码如下:

clc
close all
clear
%% 函数说明-绘制云雨图

pathFigure= '.\Figures\' ;
figureUnits = 'centimeters';
figureWidth = 20; 
figureHeight = 10;

% 导入数据
X1=[normrnd(8,4,1,120),normrnd(5,2,1,25)];
X2=[normrnd(2.5,3,1,75),normrnd(6,4,1,25),normrnd(15,1,1,100)];
X3=[normrnd(4,3,1,40),normrnd(3,4,1,25)];
X4=[normrnd(4,3,1,40),normrnd(2,4,1,75)];

dataCell={X1,X2,X3,X4};           % 把数据放到元胞数组,要是数据太多可写循环放入  
dataName={'A','B','C','D'};        % 各个数据类的名称,可空着

%% 绘制竖向云雨图
rainCloudsV(dataCell, dataName)

str= strcat(pathFigure, "图2 "+"竖向云雨图", '.tiff');
print(gcf, '-dtiff', '-r600', str);

function rainCloudsV(dataCell, dataName)

% 颜色列表
colorList=[0.9294    0.7569    0.5059
           0.9176    0.5569    0.4627
           0.7020    0.4784    0.5451
           0.4863    0.4314    0.5490];      
% =========================================================================

classNum=length(dataCell);
if size(colorList,1)==0
    colorList=repmat([130,170,172]./255,[classNum,1]);
else
    colorList=repmat(colorList,[ceil(classNum/size(colorList,1)),1]);
end
if isempty(dataName)
    for i=1:classNum
        dataName{i}=['class',num2str(i)];
    end
end

figure(2)
% 坐标区域修饰
hold on; box on;
ax=gca;
ax.XLim=[1/2,classNum+2/3];
ax.XTick=1:classNum;
ax.LineWidth=1.2;
ax.XTickLabels=dataName(end:-1:1);
ax.FontSize=14;

rate=3.5;

% 绘制雨云图
for i=1:classNum
    tX=dataCell{i};tX=tX(:);
    [F,Xi]=ksdensity(tX);

    % 绘制山脊图
    patchCell(i)=fill(0.2+[0,F,0].*rate+(classNum+1-i).*ones(1,length(F)+2),[Xi(1),Xi,Xi(end)],...
        colorList(i,:),'EdgeColor',[0,0,0],'FaceAlpha',0.8,'LineWidth',1.2);

    % 其他数据获取
    qt25=quantile(tX,0.25); % 下四分位数
    qt75=quantile(tX,0.75); % 上四分位数 
    med=median(tX);         % 中位数

    outliBool=isoutlier(tX,'quartiles');  % 离群值点
    nX=tX(~outliBool);                    % 95%置信内的数
    
    % 绘制箱线图
    plot([(classNum+1-i),(classNum+1-i)],[min(nX),max(nX)],'k','lineWidth',1.2);
    fill((classNum+1-i)+[-1 1 1 -1].*0.12,[qt25,qt25,qt75,qt75],colorList(i,:),'EdgeColor',[0 0 0]);
    plot([(classNum+1-i)-0.12,(classNum+1-i)+0.12],[med,med],'Color',[0,0,0],'LineWidth',2.5)

    % 绘制散点 
    tY=(rand(length(tX),1)-0.5).*0.24+ones(length(tX),1).*(classNum+1-i);
    scatter(tY,tX,15,'CData',colorList(i,:),'MarkerEdgeAlpha',0.15,...
        'MarkerFaceColor',colorList(i,:),'MarkerFaceAlpha',0.1)
end

set(gca,'FontName','Times New Roman','FontSize',14, 'Layer','top','LineWidth',1);

% 绘制图例
hl = legend(patchCell,dataName);
set(hl,'Box','off','location','northOutside','NumColumns',4,'FontSize',16,'FontName','Times New Roman');    
end

参考

1、微信公众号-MATLAB | 如何使用MATLAB绘制雨云图

  • 6
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WW、forever

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值