|

点击上方蓝字和“好玩的MATLAB”一起快乐玩耍吧!
* c h9 e- z4 w4 a8 |
ti1shb2z3i164012794207.jpg
9 ~! z, y2 ]2 b$ _) z0 N6 M! a好玩的matlab, T3 u& t M& U, ?
带你解锁不一样的matlab新玩法
5 f+ ^; g+ l' B, w
* @- j0 S; N+ t- I# Z前面几篇推文详细的介绍了散点图和边际图的画法【MATLAB|回归曲线|置信区间|边际图|核密度填充图、MATLAB|聚类散点图|边际图|核密度填充图】,今天特意添加边际直方图的画法,喜欢此推文的小伙伴们记得点赞+关注+分享!【尊重作者劳动成果,转载请注明推文链接和公众号名】
9 U$ i" e7 Y5 ^( w2 {, a. L效果效果图图+ y1 d! B6 _% B; w2 H
边际直方图效果图
* U# V( s; b. o4 W! m G
4ydvtkgr10j64012794307.png
/ r; u" l1 [+ p, r7 n5 e {' [
8 s2 A- _& e& q! ~
ziqdad55akn64012794408.gif
' A8 {2 o/ @2 ]: [# `9 Z! n
" S0 }$ J: K- e8 B# G
绘图工具函数classdef PlotDensHist %-------------------------------------------------------------------------- % @Author: 好玩的Matlab % @公众号:好玩的Matlab % @Created: 09,06,2023 % @Email:2377389590@qq.com % 尊重劳动成果,转载请备注推文链接和公众号名。 % @Disclaimer: This code is provided as-is without any warranty. %-------------------------------------------------------------------------- properties XData; % x数据 YData; % y数据 FillColor; % 填充颜色 FaceAlpha; % 透明度数 HFig; % 图形句柄 end methods function obj = PlotDensHist(sHdl) % 设置默认参数 obj.XData = get(sHdl, 'XData'); obj.YData = get(sHdl, 'YData'); obj.FillColor = get(sHdl, 'CData'); obj.FaceAlpha = get(sHdl, 'MarkerFaceAlpha') * 0.4;
k6 k' u: e% y" @& p$ Y obj.HFig = gcf; end function plotDensHist(obj) hFig = obj.HFig; % 获取当前figure的句柄 axesPositions = {[0.07, 0.07, 0.65, 0.65],... % 主图 [0.07, 0.07, 0.65, 0.65],... % 与主图重合 [0.07, 0.74, 0.65, 0.2],... % 顶部区域 [0.74, 0.07, 0.2, 0.65]}; % 右侧区域 axObj = findall(hFig, 'Type', 'axes'); hFig.Position = [439 76 891 790]; axesCount = length(axObj); if axesCount == 1 ax = gca; ax.Box = 'on'; ax.Position = axesPositions{1}; topAxes = axes('position', axesPositions{3}); rightAxes = axes('position', axesPositions{4}); end % 遍历所有 Axes 对象 for i = 1:length(axObj) ax = axObj(i); % 获取当前 Axes 对象 pos = get(ax, 'Position'); % 获取当前 Axes 对象的 Position 属性5 f5 {/ m' k7 Q% ^' p" m1 e
% 检查 Position 是否匹配 if sum(abs(pos - axesPositions{1})) 1e-6 mainAxes = ax; elseif sum(abs(pos - axesPositions{3})) 1e-6 topAxes = ax; elseif sum(abs(pos - axesPositions{4})) 1e-6 rightAxes = ax; end end xi = obj.XData; yi = obj.YData; nbins = round(length(xi) / 4); % 这里绘制到topAxes if ~isempty(topAxes) axes(topAxes); hold on; histogram(topAxes, xi, nbins, 'FaceColor', obj.FillColor, ... 'EdgeColor', obj.FillColor, 'FaceAlpha', obj.FaceAlpha, 'LineWidth', 0.5) topAxes.YDir = 'normal'; topAxes.XMinorTick = 'on'; topAxes.YMinorTick = 'on'; topAxes.TickDir = 'out'; topAxes.TickLength = [.011 .01]; topAxes.XTickLabel = {}; topAxes.Visible = 'on'; topAxes.Box = 'off'; topAxes.LineWidth = 1.2; topAxes.FontSize = 18; topAxes.FontName = 'Times New Roman'; topAxes.XLim = ax.XLim; topAxes.Box = 'off'; end
! v) u3 }! Y3 p% e# Y/ u% E % 这里绘制到rightAxes if ~isempty(rightAxes) axes(rightAxes); hold on; histogram(rightAxes, yi, nbins, 'FaceColor', obj.FillColor, 'Orientation', 'horizontal', ... 'EdgeColor', obj.FillColor, 'FaceAlpha', obj.FaceAlpha, 'LineWidth', 0.5) rightAxes.YDir = 'normal'; rightAxes.XMinorTick = 'on'; rightAxes.YMinorTick = 'on'; rightAxes.TickDir = 'out'; rightAxes.TickLength = [.011 .01]; rightAxes.YTickLabel = {}; rightAxes.Visible = 'on'; rightAxes.Box = 'off'; rightAxes.LineWidth = 1.2; rightAxes.FontSize = 18; rightAxes.FontName = 'Times New Roman'; rightAxes.XLim = topAxes.YLim; rightAxes.YLim = ax.YLim; rightAxes.Box = 'off'; end % 绑定X Y linkaxes([mainAxes, rightAxes], 'y') linkaxes([mainAxes, topAxes], 'x') end endend这个函数定义了PlotDensHist的MATLAB类,用于创建一个带有附加边际分布信息的直方图图形。以下是对该函数的分析和介绍:类属性:
$ Z5 A& I8 z9 @! g) t0 h6 rXData:存储主图中的X轴数据。4 |0 V, k* r# [& h- S: U1 b0 d) e
YData:存储主图中的Y轴数据。
# g1 w& _/ H, w, b. d- U4 V. OFillColor:存储直方图的填充颜色。
3 Y7 X/ T" p) q, ~FaceAlpha:存储填充区域的透明度。
8 g- m9 Q$ o7 T9 _! xHFig:存储图形句柄。
, x4 S$ z! p, b: G2 w2 K+ ~6 k- \4 \, @7 j7 d3 d( q
bofm3i4uhht64012794508.png
0 N4 N: V9 j, @4 G, k
根据句柄的形式调用。
0 }! o' z- _8 t* A" W构造函数 PlotDensHist(sHdl):7 b0 x& A; T( l/ u+ S. S* w
该构造函数接受一个图形句柄 sHdl 作为输入参数,并从该图形句柄中提取相关属性值,以初始化对象的属性。这些属性包括X轴数据、Y轴数据、填充颜色、线条颜色等。
4 A3 ^8 V' A9 k" b9 Y5 q" m J构造函数将这些属性存储在对象的属性中,并设置默认的绘图参数。/ `4 N' s: P; n
plotDensHist 方法:- B2 f- g/ C% Z8 y# O1 Q; B
plotDensHist 方法用于在MATLAB图形中创建直方图,并添加附加的边际分布信息。
- r! x" f6 K! [5 B8 F4 q3 f该方法首先获取当前图形的句柄 hFig,然后创建主图、顶部区域和右侧区域的坐标轴位置。* \4 c* C( h) ] A; b. b# B0 T5 r
主图的数据来自对象的属性 XData 和 YData,并根据对象的其他属性(如填充颜色、线条颜色等)来绘制直方图。
9 D! Q9 m! p* c& g. T2 O$ _0 N2 G顶部和右侧区域分别用于绘制边际分布的直方图,使用的数据分别是 XData 和 YData。
5 j S) D( c1 R6 I6 {方法还设置了坐标轴的各种属性,如刻度线样式、字体大小等。 W) r+ T* |2 e- o8 b
最后,通过 linkaxes 方法将主图和边际分布区域的坐标轴连接在一起,以确保它们在缩放时保持一致。: [/ n1 E/ H" Z& a, N6 E: s# Z
[/ol]案例clc;clear;close all;d1=repmat([2 2],100,1) + randn(100,2)*[1 .5; 0 1.32];d2=repmat([9 1],100,1) + randn(100,2)*[1.4 0.2; 0 0.98];d3=repmat([6 8],100,1) + randn(100,2)*[1 0.5; 0 1];8 X( g: ^3 F% G5 ]! i
% 使用scatter函数绘制散点图hold onsh1=scatter(d1(:,1), d1(:,2),'filled','CData',[0,0,1],'MarkerFaceAlpha',0.5,'MarkerEdgeColor','none','Marker','o');sh2=scatter(d2(:,1), d2(:,2),'filled','CData',[0,1,0],'MarkerFaceAlpha',0.5,'MarkerEdgeColor','none','Marker','^');sh3=scatter(d3(:,1), d3(:,2),'filled','CData',[1,0.6471,0],'MarkerFaceAlpha',0.5,'MarkerEdgeColor','none','Marker','s');) m u! L5 P+ q7 p( A
% 画出凸包k1 = convhull(d1(:,1), d1(:,2));k2 = convhull(d2(:,1), d2(:,2));k3 = convhull(d3(:,1), d3(:,2));line(d1(k1,1), d1(k1,2), 'Color', [0 0 1],'LineWidth',1.2)line(d2(k2,1), d2(k2,2), 'Color', [0,1,0],'LineWidth',1.2)line(d3(k3,1), d3(k3,2), 'Color', [1,0.6471,0],'LineWidth',1.2)legend('demo1','demo2','demo3','box','off','Location','best')% 设置标签和标题ax=gca;box onax.XLim=[-2,15];ax.YLim=[-4,13];ax.XLabel.String='X';ax.YLabel.String='Y';ax.Title.String='';, r0 i2 @ \6 x8 Z0 R4 X
ax.GridLineStyle = '-.'; % 设置网格线样式为虚线ax.GridColor = 'k'; % 设置网格线颜色为红色ax.XGrid = 'off'; % 关闭X网格线ax.YGrid = 'off'; % 打开Y网格线5 c2 t9 o9 b4 Y: W, q, c
ax.LineWidth = 1; % 设置坐标线宽ax.XMinorTick = 'on'; % 打开x次要刻度线ax.YMinorTick = 'on'; % 打开y次要刻度线ax.TickDir = 'out'; % 设置刻度线方向向外ax.FontSize = 14; % 设置坐标字体大小9 j0 Y$ X! U* g5 N! ^
Phdl1=PlotDensHist(sh1);Phdl1.plotDensHist();Phdl2=PlotDensHist(sh2);Phdl2.plotDensHist();Phdl3=PlotDensHist(sh3);Phdl3.plotDensHist();
6 ?& i+ g! O, T) ?: W4 D0 X) e# o
sd4viuzz0r164012794608.png
3 P0 Z0 Q0 Z9 f* H Y
- -THE END- -
$ s$ z) R! T- l1 R% f源码下载:gitee下载:https://gitee.com/iDMatlab/cool-scatter-chart* ~( f0 @$ B- w- O
oryft0113yy64012794708.png
; {& i" a0 F+ Z. c6 m参考资料:
1 `- I8 I5 d* R- i! Z【1】MATLAB|炫酷的聚类散点图【2】MATLAB|聚类散点图|边际图|核密度填充图【3】MATLAB|回归曲线|置信区间|边际图|核密度填充图3 u' \' K1 H4 |8 v/ k; \
oksffapfm0h64012794808.gif
7 s3 ?0 g6 E2 M3 \8 t$ w1 ?& R
vaudkda1gas64012794908.png
+ i( S4 Q$ {* D送书活动
4 i) `7 G' G5 t% J) c N4 F
tikdcbzxncj64012795008.png
7 X: X6 M. }: s
jof1pg3yris64012795108.gif
6 F' E+ w' L: O' Q$ H- b ?5 F" Q4 Z" m8 W# g6 F
包邮赠送 「北京大学出版社」赞助《MATLAB科学计算从入门到精通》
$ s2 _5 K" z, e# n- Y本书从 MATLAB 基础语法讲起,介绍了基于 MATLAB 函数的科学计算问题求解方法,实现了大量科学计算算法。
: r8 w9 u5 O# I/ s本书分为三大部分。第 1 章和第 2 章为 MATLAB 的基础知识,对全书用到的 MATLAB 基础进行了简单介绍。第 3 ~ 12 章为本书的核心部分,包括线性方程组求解、非线性方程求解、数值优化、数据插值、数据拟合与回归分析、数值积分、常微分方程求解、偏微分方程求解、概率统计计算及图像处理与信号处理等内容。第 13 ~ 15 章为实战部分,以实际生活中的数学问题为例,将前文介绍的各类科学计算算法应用其中。本书内容全面、通俗易懂,适合有一定 MATLAB 基础、想要进行进阶学习的读者。
) [8 y' ~% m; \! S- l了解更多
( w- g+ P& ~9 {▼▼▼) g% @( _3 V; W0 m
2qgxkheizoa64012795208.jpg
4 V: b/ D3 V C8 @
' I2 D. v) v/ x4 V& ~
【抽奖方式及满足条件】:
! R/ r8 V7 b4 {9 U$ d0 ]4 m$ f1.关注「好玩的MATLAB 」公众号和视频号
; k E: X8 \+ Y& J& c( R$ X
5qni1mvp1z264012795308.jpg
i1 G% n0 Q( R2.给本文点【赞】+【在看】;0 U( }! K/ J$ |
3.留言区评论点赞最多的前3名。
0 R9 f1 n/ v; e# H) e6 F* t4.本活动只针对从未获过奖的同学,之前获过奖的小伙伴,不用参加。
% y/ g, Q5 V* v4 S同时满足上述4个条件的读者朋友,包邮赠送一本
, `- r0 O, {% F+ G$ k! U- Y【开奖时间】:2023年10月10日夜晚8点
8 _, G; r7 m% W' m1 ?& F【领奖方式】:在开奖时加小编私人微信:idmatlab
, Q f9 a7 C$ n: r& a扫一扫加管理员微信5 T% Z+ T' O! e8 A2 K# e
0fegaexfewi64012795408.png
- @! ?5 V' g9 k* V* m) h
ysjbodx2ahp64012795508.jpg
|
|