Matlab繪制雨云圖的方法詳解
介紹
寫了倆代碼模板,用來(lái)繪制橫向云雨圖與縱向云雨圖,云雨圖其實(shí)就是用把小提琴圖拆開來(lái)的模板,想獲取小提琴圖繪制函數(shù)的可以看這里:基于Matlab繪制小提琴圖的示例代碼
后面的倆模板用的時(shí)候只需要換換數(shù)據(jù),顏色及每一類名稱即可,雨云圖繪制效果如下:
橫向雨云圖
function rainCloudsTMPL1 % @author: slandarer % 在這里放入你的數(shù)據(jù)======================================================= 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}; % 把數(shù)據(jù)放到元胞數(shù)組,要是數(shù)據(jù)太多可寫循環(huán)放入 dataName={'A','B','C','D'}; % 各個(gè)數(shù)據(jù)類的名稱,可空著 % 顏色列表 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 % 坐標(biāo)區(qū)域修飾 hold 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); % 其他數(shù)據(jù)獲取 qt25=quantile(tX,0.25); % 下四分位數(shù) qt75=quantile(tX,0.75); % 上四分位數(shù) med=median(tX); % 中位數(shù) outliBool=isoutlier(tX,'quartiles'); % 離群值點(diǎn) nX=tX(~outliBool); % 95%置信內(nèi)的數(shù) % 繪制箱線圖 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) % 繪制散點(diǎn) 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 % 繪制圖例 lgd=legend(patchCell,dataName); lgd.Location='best'; end
縱向雨云圖
function rainCloudsTMPL2 % @author: slandarer % 在這里放入你的數(shù)據(jù)======================================================= 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}; % 把數(shù)據(jù)放到元胞數(shù)組,要是數(shù)據(jù)太多可寫循環(huán)放入 dataName={'A','B','C','D'}; % 各個(gè)數(shù)據(jù)類的名稱,可空著 % 顏色列表 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 % 坐標(biāo)區(qū)域修飾 hold 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); % 其他數(shù)據(jù)獲取 qt25=quantile(tX,0.25); % 下四分位數(shù) qt75=quantile(tX,0.75); % 上四分位數(shù) med=median(tX); % 中位數(shù) outliBool=isoutlier(tX,'quartiles'); % 離群值點(diǎn) nX=tX(~outliBool); % 95%置信內(nèi)的數(shù) % 繪制箱線圖 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) % 繪制散點(diǎn) 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 % 繪制圖例 lgd=legend(patchCell,dataName); lgd.Location='best'; end
到此這篇關(guān)于Matlab繪制雨云圖的方法詳解的文章就介紹到這了,更多相關(guān)Matlab雨云圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語(yǔ)言實(shí)現(xiàn)個(gè)稅計(jì)算器
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)個(gè)稅計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)超詳細(xì)講解單向鏈表
鏈表可以說(shuō)是一種最為基礎(chǔ)的數(shù)據(jù)結(jié)構(gòu)了,而單向鏈表更是基礎(chǔ)中的基礎(chǔ)。鏈表是由一組元素以特定的順序組合或鏈接在一起的,不同元素之間在邏輯上相鄰,但是在物理上并不一定相鄰。在維護(hù)一組數(shù)據(jù)集合時(shí),就可以使用鏈表,這一點(diǎn)和數(shù)組很相似2022-03-03C語(yǔ)言實(shí)現(xiàn)點(diǎn)菜系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)點(diǎn)菜系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)易版三子棋游戲
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)易版三子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07詳解Ubuntu18.04配置VSCode+CMake的C++開發(fā)環(huán)境
這篇文章主要介紹了詳解Ubuntu18.04配置VSCode+CMake的C++開發(fā)環(huán)境,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03C++中關(guān)于=default和=delete問(wèn)題
這篇文章主要介紹了C++中關(guān)于=default和=delete問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07