基于Matlab繪制小提琴圖的示例代碼
本文將為大家詳細(xì)講解Matlab中小提琴圖的繪制函數(shù)及ggtheme主題修飾函數(shù)

violinChart 函數(shù)使用方法
寫(xiě)了個(gè)matlab繪制小提琴圖的函數(shù):

1.圖中小提琴狀區(qū)域?yàn)楹嗣芏惹€(xiàn)。
2.白色方塊為25%,75%分位數(shù)。
3.中間橫線(xiàn)為中位數(shù)。
4.白色點(diǎn)為離群值點(diǎn)。
5.豎著的黑線(xiàn)是去掉離群值點(diǎn)后點(diǎn)的上下限。
基礎(chǔ)使用,Y為矩陣
X=1:5; Y=randn(100,5); violinChart(gca,X,Y,[0 0.447 0.741],0.6);

- X | 橫坐標(biāo)。
- Y | 數(shù)據(jù)集。
- FaceColor | 顏色,示例用的是[0 0.447 0.741]。
- width | 小提琴圖寬度,這里取的是0.6,就是以?xún)蓚€(gè)小提琴圖間距的0.6倍為概率密度的上限1。
基礎(chǔ)使用,Y為向量,X為標(biāo)簽
X=[1.*ones(1,50),2.*ones(1,30),3.*ones(1,20),4.*ones(1,50),5.*ones(1,50)]; Y=randn(1,200)+sin(X); violinChart(gca,X,Y,[0 0.447 0.741]);

基礎(chǔ)使用,多個(gè)圖像繪制,并添加圖例
X1=[1:2:7,13];
Y1=randn(100,5)+sin(X1);
X2=2:2:10;
Y2=randn(100,5)+cos(X2);
figure
Hdl1=violinChart(gca,X1,Y1,[0 0.447 0.741]);
Hdl2=violinChart(gca,X2,Y2,[0.850 0.325 0.098]);
legend([Hdl1.F_legend,Hdl2.F_legend],{'randn+sin(x)','randn+cos(x)'});
violinChart 完整函數(shù)
若函數(shù)有更新則會(huì)將更新版本放入文末所示壓縮文件內(nèi)。
function Hdl=violinChart(ax,X,Y,FaceColor,width)
% @author slandarer
% Hdl: 返回的圖形對(duì)象句柄結(jié)構(gòu)體
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% Hdl.F_density(i) | patch | 核密度分布
% Hdl.F_outlier(i) | scatter | 離群值點(diǎn)
% Hdl.F_range95(i) | line | 去除離群值點(diǎn)后最大值及最小值
% Hdl.F_quantile(i) | patch | 四分位數(shù)框
% Hdl.F_medianLine(i)| line | 中位數(shù)
%
% Hdl.F_legend | patch | 用于生成legend圖例的圖形對(duì)象
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% 請(qǐng)使用如下方式生成圖例:
% Hdl1=violinChart(ax,X,Y,... ...)
% Hdl2=violinChart(ax,X,Y,... ...)
% ... ...
% legend([Hdl1,Hdl2,... ...],{Name1,Name2,...})
% ===========================================================
% 以下為使用實(shí)例代碼:
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% X1=[1:2:7,13];
% Y1=randn(100,5)+sin(X1);
% X2=2:2:10;
% Y2=randn(100,5)+cos(X2);
%
% Hdl1=violinChart(gca,X1,Y1,[0 0.447 0.741],0.5);
% Hdl2=violinChart(gca,X2,Y2,[0.850 0.325 0.098],0.5);
% legend([Hdl1.F_legend,Hdl2.F_legend],{'randn+sin(x)','randn+cos(x)'});
if nargin<5
width=0.4;
end
if ~isempty(ax)
else
ax=gca;
end
hold(ax,'on');
oriX=X;
X=unique(X);
sep=min(diff(X));
if isempty(sep)
sep=1;
end
for i=1:length(X)
if length(oriX)==numel(Y)
tY=Y(oriX==X(i));
else
tY=Y(:,i);
end
[f,yi]=ksdensity(tY);
Hdl.F_density(i)=fill([f,-f(end:-1:1)].*sep.*width+X(i),[yi,yi(end:-1:1)],FaceColor);
outliBool=isoutlier(tY,'quartiles');
outli=tY(outliBool);
Hdl.F_outlier(i)=scatter(repmat(X(i),[length(outli),1]),outli,20,'filled',...
'CData',[1 1 1],'MarkerEdgeColor','none');
nY=tY(~outliBool);
Hdl.F_range95(i)=plot([X(i),X(i)],[min(nY),max(nY)],'k','lineWidth',1);
qt25=quantile(tY,0.25);
qt75=quantile(tY,0.75);
Hdl.F_quantile(i)=fill(X(i)+0.6.*sep.*width.*[-1 1 1 -1].*max(f),...
[qt25,qt25,qt75,qt75],[1 1 1],...
'EdgeColor',[0 0 0]);
med=median(tY);
Hdl.F_medianLine(i)=plot(X(i)+0.6.*sep.*width.*[-1 1].*max(f),[med,med],'LineWidth',3,...
'Color',[0 0 0]);
end
Hdl.F_legend=Hdl.F_density(1);
endggtheme violin 函數(shù)介紹
假設(shè)你已經(jīng)編寫(xiě)了上述繪圖代碼,只需要將最后增添一行變?yōu)?/p>
X1=[1:2:7,13];
Y1=randn(100,5)+sin(X1);
X2=2:2:10;
Y2=randn(100,5)+cos(X2);
figure
Hdl1=violinChart(gca,X1,Y1,[0 0.447 0.741]);
Hdl2=violinChart(gca,X2,Y2,[0.850 0.325 0.098]);
legend([Hdl1.F_legend,Hdl2.F_legend],{'randn+sin(x)','randn+cos(x)'});
ggThemeViolin(gca,[Hdl1,Hdl2],'dust');則圖像會(huì)被修飾:

而將函數(shù)最后加入‘LP’參數(shù)則變?yōu)椋?/p>
ggThemeViolin(gca,[Hdl1,Hdl2],'dust','LP');

ggtheme violin 主題
主題有如下選擇:
'flat'/'flat_dark'/'camouflage'/'chalk'/
'copper'/'dust'/'earth'/'fresh'/'grape'/
'grass'/'greyscale'/'light'/'lilac'/'pale'/
'sea'/'sky'/'solarized'
'flat'

'flat_dark'

'camouflage'

'chalk'

'copper'

'dust'

'earth'

'fresh'

'grape'

'grass'

'greyscale'

'light'

'lilac'

'pale'

'sea'

'sky'

'solarized'

ggtheme violin 修飾函數(shù)代碼
注意,要使用ggThemeViolin修飾器函數(shù),需要保證當(dāng)前文件夾有themeCSS.mat文件,該文件將會(huì)一同在壓縮包內(nèi)給出。
function ax=ggThemeViolin(varargin)
% @author:slandarer
%
% 參數(shù)說(shuō)明:
% -----------------------------------------------------
% AxesTheme | 坐標(biāo)區(qū)域風(fēng)格 | 'flat'/'flat_dark'/'camouflage'/'chalk'/
% 'copper'/'dust'/'earth'/'fresh'/'grape'/
% 'grass'/'greyscale'/'light'/'lilac'/'pale'
% 'sea'/'sky'/'solarized'
%
% HDLset | 句柄集合
% 獲取要處理的坐標(biāo)區(qū)域=====================================================
if strcmp(get(varargin{1},'type'),'axes' )
ax=varargin{1};
else
ax=gca;
end
hold(ax,'on')
% 獲取要處理的圖像句柄=====================================================
HDLset=varargin{2};
% 獲取風(fēng)格名稱(chēng)=============================================================
theme.AxesTheme='flat';
if length(varargin)>2
theme.AxesTheme=varargin{3};
end
% 開(kāi)始風(fēng)格化===============================================================
ax.Box='off';
ax.YGrid='on';
ax.XGrid='on';
ax.GridLineStyle='--';
ax.LineWidth=1.2;
% 主題風(fēng)格化
Tm=load('themeCSS.mat');
Tm=Tm.theme;
ax.Color=Tm.(theme.AxesTheme).Color;
ax.TickLength=Tm.(theme.AxesTheme).TickLength;
ax.GridColorMode=Tm.(theme.AxesTheme).GridColorMode;
ax.GridColor=Tm.(theme.AxesTheme).GridColor;
ax.GridAlpha=Tm.(theme.AxesTheme).GridAlpha;
ax.XColor=Tm.(theme.AxesTheme).XColor;
ax.YColor=Tm.(theme.AxesTheme).YColor;
ax.TickDir=Tm.(theme.AxesTheme).TickDir;
ax.ColorOrder=Tm.(theme.AxesTheme).ColorOrder;
for i=1:length(HDLset)
for j=1:length(HDLset(i).F_density)
HDLset(i).F_density(j).FaceColor=ax.ColorOrder(mod(i-1,size(ax.ColorOrder,1))+1,:);
HDLset(i).F_density(j).EdgeColor=[.1,.1,.1];
f_max=(max(HDLset(i).F_density(j).XData)-min(HDLset(i).F_density(j).XData))/2;
x_mid=(max(HDLset(i).F_density(j).XData)+min(HDLset(i).F_density(j).XData))/2;
HDLset(i).F_quantile(j).XData=x_mid+0.4.*f_max.*[-1 1 1 -1];
HDLset(i).F_quantile(j).FaceColor=[1 1 1].*0.95;
HDLset(i).F_medianLine(j).XData=x_mid+0.4.*f_max.*[-1 1];
HDLset(i).F_medianLine(j).LineWidth=2;
HDLset(i).F_medianLine(j).Color=[0.3,0.3,0.3];
HDLset(i).F_outlier(j).CData=Tm.(theme.AxesTheme).EdgeColor;
end
end
if ~isempty(ax.Legend)
ax.Legend.Box='off';
ax.Legend.FontSize=12;
if mean(ax.Color)>0.6
ax.Legend.TextColor=ax.XColor;
else
ax.Legend.TextColor=[0.9 0.9 0.9];
end
if ~isempty(regexpi(ax.Legend.Location,'out', 'once'))
ax.Legend.TextColor=ax.XColor;
ax.Legend.Title.FontSize=14;
end
ax.Legend.AutoUpdate='off';
end
end到此這篇關(guān)于基于Matlab繪制小提琴圖的示例代碼的文章就介紹到這了,更多相關(guān)Matlab小提琴圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于C語(yǔ)言實(shí)現(xiàn)創(chuàng)意多彩貪吃蛇游戲
這篇文章主要介紹了如何利用C語(yǔ)言實(shí)現(xiàn)一個(gè)創(chuàng)意多彩貪吃蛇游戲,這是一個(gè)純C語(yǔ)言外加easyx庫(kù)的繪圖函數(shù)制作而成的有趣小游戲,無(wú)需引入額外資源,感興趣的可以動(dòng)手嘗試一下2022-08-08
Typedef在C語(yǔ)言和C++中的用法和區(qū)別
在C語(yǔ)言和C++中,typedef是一個(gè)非常常用的關(guān)鍵字,用于為數(shù)據(jù)類(lèi)型定義別名,盡管它在兩種語(yǔ)言中都有相似的功能,但由于C++具有更豐富的類(lèi)型系統(tǒng),因此在實(shí)際應(yīng)用中,typedef在兩者間的使用存在一些微妙的差異2024-01-01
c++ vector模擬實(shí)現(xiàn)的全過(guò)程
這篇文章主要給大家介紹了關(guān)于c++ vector的模擬實(shí)現(xiàn)過(guò)程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
詳解C語(yǔ)言在STM32中的內(nèi)存分配問(wèn)題
這篇文章主要介紹了C語(yǔ)言在STM32中的內(nèi)存分配,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12
C++項(xiàng)目求Fibonacci數(shù)列的參考解答
今天小編就為大家分享一篇關(guān)于C++項(xiàng)目求Fibonacci數(shù)列的參考解答,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-02-02
C語(yǔ)言文件操作函數(shù)freopen詳細(xì)解析
替換一個(gè)流,或者說(shuō)重新分配文件指針,實(shí)現(xiàn)重定向。如果stream流已經(jīng)打開(kāi),則先關(guān)閉該流。如果該流已經(jīng)定向,則freopen將會(huì)清除該定向。此函數(shù)一般用于將一個(gè)指定的文件打開(kāi)一個(gè)預(yù)定義的流:標(biāo)準(zhǔn)輸入、標(biāo)準(zhǔn)輸出或者標(biāo)準(zhǔn)出錯(cuò)2013-10-10
C/C++多參數(shù)函數(shù)參數(shù)的計(jì)算順序與壓棧順序的示例代碼
這篇文章主要介紹了C/C++多參數(shù)函數(shù)參數(shù)的計(jì)算順序與壓棧順序,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06

