網站首頁 編程語言 正文
效果
原理也很簡單,通過matlab自帶的ksdensity獲得網格每一點密度,通過密度擬合曲面,再計算每個數據點對應的概率,并將概率映射到顏色即可
為了怕大家找不到函數這次工具函數放到最前面
1工具函數完整代碼
function [CData,h,XMesh,YMesh,ZMesh,colorList]=density2C(X,Y,XList,YList,colorList) [XMesh,YMesh]=meshgrid(XList,YList); XYi=[XMesh(:) YMesh(:)]; F=ksdensity([X,Y],XYi); ZMesh=zeros(size(XMesh)); ZMesh(1:length(F))=F; h=interp2(XMesh,YMesh,ZMesh,X,Y); if nargin<5 colorList=[0.2700 0 0.3300 0.2700 0.2300 0.5100 0.1900 0.4100 0.5600 0.1200 0.5600 0.5500 0.2100 0.7200 0.4700 0.5600 0.8400 0.2700 0.9900 0.9100 0.1300]; end colorFunc=colorFuncFactory(colorList); CData=colorFunc((h-min(h))./(max(h)-min(h))); colorList=colorFunc(linspace(0,1,100)'); function colorFunc=colorFuncFactory(colorList) x=(0:size(colorList,1)-1)./(size(colorList,1)-1); y1=colorList(:,1);y2=colorList(:,2);y3=colorList(:,3); colorFunc=@(X)[interp1(x,y1,X,'pchip'),interp1(x,y2,X,'pchip'),interp1(x,y3,X,'pchip')]; end end
2參數說明
輸入:
- X,Y 散點坐標
- XList,YList 用來構造密度曲面網格的序列,其實就是把XLim,YLim分成小份,例如XList=0:0.1:10
- colorList 顏色表mx3數組,用來構造將高度映射到顏色函數的數據表
輸出:
- CData各個點對應顏色
- h 各個點對應核密度
- XMesh,YMesh,ZMesh 核密度曲面數據
- colorList 插值后更細密的顏色表
3使用方式
假如編寫了如下程序:
PntSet1=mvnrnd([2 3],[1 0;0 2],800); PntSet2=mvnrnd([6 7],[1 0;0 2],800); PntSet3=mvnrnd([8 9],[1 0;0 1],800); PntSet=[PntSet1;PntSet2;PntSet3]; scatter(PntSet(:,1),PntSet(:,2),'filled');
結果:
3.1散點賦色
將上面那段代碼改寫
PntSet1=mvnrnd([2 3],[1 0;0 2],800); PntSet2=mvnrnd([6 7],[1 0;0 2],800); PntSet3=mvnrnd([8 9],[1 0;0 1],800); PntSet=[PntSet1;PntSet2;PntSet3]; CData=density2C(PntSet(:,1),PntSet(:,2),-2:0.1:15,-2:0.1:15); scatter(PntSet(:,1),PntSet(:,2),'filled','CData',CData);
3.2等高線圖
PntSet1=mvnrnd([2 3],[1 0;0 2],800); PntSet2=mvnrnd([6 7],[1 0;0 2],800); PntSet3=mvnrnd([8 9],[1 0;0 1],800); PntSet=[PntSet1;PntSet2;PntSet3]; [~,~,XMesh,YMesh,ZMesh,colorList]=density2C(PntSet(:,1),PntSet(:,2),-2:0.1:12,-2:0.1:12); colormap(colorList) contourf(XMesh,YMesh,ZMesh,10)
3.3帶直方圖的散點圖
PntSet1=mvnrnd([2 3],[1 0;0 2],800); PntSet2=mvnrnd([6 7],[1 0;0 2],800); PntSet3=mvnrnd([8 9],[1 0;0 1],800); PntSet=[PntSet1;PntSet2;PntSet3]; colorList=[0.9400 0.9700 0.9600 0.8900 0.9300 0.9200 0.8200 0.9100 0.8800 0.6900 0.8500 0.7700 0.5900 0.7800 0.6900 0.5500 0.7500 0.6500 0.4500 0.6500 0.5600 0.4000 0.5800 0.4900 0.3500 0.5100 0.4200 0.2500 0.3600 0.3100 0.1300 0.1700 0.1400]; CData=density2C(PntSet(:,1),PntSet(:,2),-2:0.1:15,-2:0.1:15,colorList); set(gcf,'Color',[1 1 1]); % 主分布圖 ax1=axes('Parent',gcf);hold(ax1,'on') scatter(ax1,PntSet(:,1),PntSet(:,2),'filled','CData',CData); ax1.Position=[0.1,0.1,0.6,0.6]; % X軸直方圖 ax2=axes('Parent',gcf);hold(ax2,'on') histogram(ax2,PntSet(:,1),'FaceColor',[0.78 0.88 0.82],... 'EdgeColor','none','FaceAlpha',0.7) ax2.Position=[0.1,0.75,0.6,0.15]; ax2.YColor='none'; ax2.XTickLabel=''; ax2.TickDir='out'; ax2.XLim=ax1.XLim; % Y軸直方圖 ax3=axes('Parent',gcf);hold(ax3,'on') histogram(ax3,PntSet(:,2),'FaceColor',[0.78 0.88 0.82],... 'EdgeColor','none','FaceAlpha',0.7,'Orientation','horizontal') ax3.Position=[0.75,0.1,0.15,0.6]; ax3.XColor='none'; ax3.YTickLabel=''; ax3.TickDir='out'; ax3.YLim=ax1.YLim;
3.4帶直方圖的等高線圖
PntSet1=mvnrnd([2 3],[1 0;0 2],800); PntSet2=mvnrnd([6 7],[1 0;0 2],800); PntSet3=mvnrnd([8 9],[1 0;0 1],800); PntSet=[PntSet1;PntSet2;PntSet3]; colorList=[0.9300 0.9500 0.9700 0.7900 0.8400 0.9100 0.6500 0.7300 0.8500 0.5100 0.6200 0.7900 0.3700 0.5100 0.7300 0.2700 0.4100 0.6300 0.2100 0.3200 0.4900 0.1500 0.2200 0.3500 0.0900 0.1300 0.2100 0.0300 0.0400 0.0700]; [~,~,XMesh,YMesh,ZMesh,colorList]=density2C(PntSet(:,1),PntSet(:,2),-2:0.1:13,-2:0.1:13,colorList); set(gcf,'Color',[1 1 1]); % 主分布圖 ax1=axes('Parent',gcf);hold(ax1,'on') colormap(colorList) contourf(XMesh,YMesh,ZMesh,10,'EdgeColor','none') ax1.Position=[0.1,0.1,0.6,0.6]; ax1.TickDir='out'; % X軸直方圖 ax2=axes('Parent',gcf);hold(ax2,'on') [f,xi]=ksdensity(PntSet(:,1)); fill([xi,xi(1)],[f,0],[0.34 0.47 0.71],'FaceAlpha',... 0.3,'EdgeColor',[0.34 0.47 0.71],'LineWidth',1.2) ax2.Position=[0.1,0.75,0.6,0.15]; ax2.YColor='none'; ax2.XTickLabel=''; ax2.TickDir='out'; ax2.XLim=ax1.XLim; % Y軸直方圖 ax3=axes('Parent',gcf);hold(ax3,'on') [f,yi]=ksdensity(PntSet(:,2)); fill([f,0],[yi,yi(1)],[0.34 0.47 0.71],'FaceAlpha',... 0.3,'EdgeColor',[0.34 0.47 0.71],'LineWidth',1.2) ax3.Position=[0.75,0.1,0.15,0.6]; ax3.XColor='none'; ax3.YTickLabel=''; ax3.TickDir='out'; ax3.YLim=ax1.YLim;
4使用方式擴展–與ggplot修飾器聯動
ggplot風格修飾器:(點擊圖片跳轉鏈接)
示例1
PntSet1=mvnrnd([2 3],[1 0;0 2],800); PntSet2=mvnrnd([6 7],[1 0;0 2],800); PntSet3=mvnrnd([8 9],[1 0;0 1],800); PntSet=[PntSet1;PntSet2;PntSet3]; ax=gca; ax.XLim=[-1 13]; ax.YLim=[-1 13]; ax=ggplotAxes2D(ax); CData=density2C(PntSet(:,1),PntSet(:,2),0:0.1:15,0:0.1:15); scatter(PntSet(:,1),PntSet(:,2),'filled','CData',CData);
是不是瞬間有那味了:
示例2
PntSet1=mvnrnd([2 3],[1 0;0 2],800); PntSet2=mvnrnd([6 7],[1 0;0 2],800); PntSet3=mvnrnd([8 9],[1 0;0 1],800); PntSet=[PntSet1;PntSet2;PntSet3]; ax=gca; ax.XLim=[-3 13]; ax.YLim=[-3 13]; ax=ggplotAxes2D(ax); [~,~,XMesh,YMesh,ZMesh,colorList]=density2C(PntSet(:,1),PntSet(:,2),-2:0.1:12,-2:0.1:12); colormap(colorList) contourf(XMesh,YMesh,ZMesh,10)
原文鏈接:https://blog.csdn.net/slandarer/article/details/120242042
相關推薦
- 2023-07-04 spring boot security自定義認證
- 2022-07-13 常用類之BigDecimal、Date、Calender、SimpleDateFormat及Syst
- 2022-11-21 Android四大組件之broadcast廣播詳解_Android
- 2023-03-26 CSS填充和寬高詳解_基礎教程
- 2023-01-23 重啟后nvidia-smi命令不可執行出現“Make?sure?that?the?latest?NV
- 2022-10-11 利用React實現虛擬列表的示例代碼_React
- 2022-11-06 Android?菜單欄DIY實現效果詳解_Android
- 2023-10-13 is using incorrect casing. Use PascalCase for Reac
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支