|
点击上方蓝字和“好玩的MATLAB”一起快乐玩耍吧!4 o5 |1 H" b5 d7 s2 N1 U4 b. R
kipp045v5ms64027198454.jpg
! e1 I' v& P% r
好玩的matlab
1 k! {: n$ e# v& W带你解锁不一样的matlab新玩法
m) ]) K: }9 E# K6 i- d) R. Z9 D% P7 q+ g
前面几篇推文详细的介绍了散点图和边际图的画法【MATLAB|回归曲线|置信区间|边际图|核密度填充图、MATLAB|聚类散点图|边际图|核密度填充图】,今天特意添加边际直方图的画法,喜欢此推文的小伙伴们记得点赞+关注+分享!【尊重作者劳动成果,转载请注明推文链接和公众号名】
2 z( n* T, @* i效果效果图图9 ]7 |: @ z7 H# {) g3 B
边际直方图效果图
* j t: B5 q9 {9 L
1qaxgjs0bzu64027198554.png
' a; M' {, Y5 a; @! J
7 d$ Z/ o& X3 J7 ]" F! A. d0 p
21ph5cyavtb64027198655.gif
5 `* {4 r! _. S0 \$ r; O: Z* ^% B5 G# e5 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;
F9 h% z t& q; U8 D, O 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 属性
6 c* p/ @) a& X; w" t& t % 检查 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
- o8 ?1 E. `% c1 `8 T7 M2 ` m, H % 这里绘制到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类,用于创建一个带有附加边际分布信息的直方图图形。以下是对该函数的分析和介绍:类属性:$ o! T2 m# M. N
XData:存储主图中的X轴数据。
0 m- l4 \6 D3 [0 W9 P' d0 H, b' N/ k% J+ xYData:存储主图中的Y轴数据。
$ l* j. w8 p* L" s" X9 E' c7 x9 ]FillColor:存储直方图的填充颜色。
+ e! m- k) `) z+ tFaceAlpha:存储填充区域的透明度。# l) H) b k* }$ L
HFig:存储图形句柄。( b# ?& Q _0 U7 P6 u3 A5 @+ F$ P
* V. \9 e8 S3 T" H& V7 m# c/ k: f
j3djps5skmo64027198755.png
$ \ m3 O, u% S, S7 Z J* {8 V
根据句柄的形式调用。2 c- N' o: z+ s; N
构造函数 PlotDensHist(sHdl):& [0 k" B+ v7 A+ _' t/ b( Q
该构造函数接受一个图形句柄 sHdl 作为输入参数,并从该图形句柄中提取相关属性值,以初始化对象的属性。这些属性包括X轴数据、Y轴数据、填充颜色、线条颜色等。3 }& B P: Z2 d% t2 m
构造函数将这些属性存储在对象的属性中,并设置默认的绘图参数。( y& i# M0 q# e6 I! v' z7 N
plotDensHist 方法:7 K) W4 {7 l, s* n6 Z# t# l
plotDensHist 方法用于在MATLAB图形中创建直方图,并添加附加的边际分布信息。
" \! C. F1 U9 b* s7 P3 h1 I+ J2 E该方法首先获取当前图形的句柄 hFig,然后创建主图、顶部区域和右侧区域的坐标轴位置。" v. m" E4 E$ M" g( _( {
主图的数据来自对象的属性 XData 和 YData,并根据对象的其他属性(如填充颜色、线条颜色等)来绘制直方图。0 Y" ^* n& R( \8 P9 v' {! S% y
顶部和右侧区域分别用于绘制边际分布的直方图,使用的数据分别是 XData 和 YData。0 X9 o) ?# i( t2 O; x" @9 _9 K. w
方法还设置了坐标轴的各种属性,如刻度线样式、字体大小等。. R( o- v# \ E0 E; T! t5 t/ E& }. q
最后,通过 linkaxes 方法将主图和边际分布区域的坐标轴连接在一起,以确保它们在缩放时保持一致。
" H& c' b# k g8 J: u1 P[/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];
0 c( P; {' T7 j9 T' ~% \% 使用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');* I2 r& ^% ]7 p# }: m* h
% 画出凸包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='';. x- n0 ] m. O1 e: }
ax.GridLineStyle = '-.'; % 设置网格线样式为虚线ax.GridColor = 'k'; % 设置网格线颜色为红色ax.XGrid = 'off'; % 关闭X网格线ax.YGrid = 'off'; % 打开Y网格线
5 R( M/ H+ p7 h& {5 H$ x. Uax.LineWidth = 1; % 设置坐标线宽ax.XMinorTick = 'on'; % 打开x次要刻度线ax.YMinorTick = 'on'; % 打开y次要刻度线ax.TickDir = 'out'; % 设置刻度线方向向外ax.FontSize = 14; % 设置坐标字体大小 Y5 e: s9 k; E& b, g
Phdl1=PlotDensHist(sh1);Phdl1.plotDensHist();Phdl2=PlotDensHist(sh2);Phdl2.plotDensHist();Phdl3=PlotDensHist(sh3);Phdl3.plotDensHist();! m( C' I- u' X- F! D
vbpptfpuyba64027198855.png
3 T- S% W# A L: O
- -THE END- -
- W, ^3 N0 z# |! h( a8 p源码下载:gitee下载:https://gitee.com/iDMatlab/cool-scatter-chart8 H! Q! |" C9 a6 H
eoitymar1wo64027198955.png
: B$ Y5 d/ @ ]& u7 b0 h( i& D
参考资料:! H. G+ ?. ?6 P4 D0 \
【1】MATLAB|炫酷的聚类散点图【2】MATLAB|聚类散点图|边际图|核密度填充图【3】MATLAB|回归曲线|置信区间|边际图|核密度填充图
* }; W, b7 Z& N7 S' ]8 q9 h/ x8 I
1u2wnecuw0x64027199055.gif
2 J- p% T% u C v" L1 R' b- H
bywox5jspuv64027199155.png
% K) d4 I' X: [送书活动
3 g* t! o- W: ~- i
q23u01nfarx64027199255.png
w2 L* F0 ]- b+ k$ v
bxgbh3friyi64027199355.gif
1 a5 f4 N$ b9 V* Z8 y
n1 i0 O- A5 Y包邮赠送 「北京大学出版社」赞助《MATLAB科学计算从入门到精通》: m* I+ t9 a. D( u4 w
本书从 MATLAB 基础语法讲起,介绍了基于 MATLAB 函数的科学计算问题求解方法,实现了大量科学计算算法。$ w' r# a# ^0 A
本书分为三大部分。第 1 章和第 2 章为 MATLAB 的基础知识,对全书用到的 MATLAB 基础进行了简单介绍。第 3 ~ 12 章为本书的核心部分,包括线性方程组求解、非线性方程求解、数值优化、数据插值、数据拟合与回归分析、数值积分、常微分方程求解、偏微分方程求解、概率统计计算及图像处理与信号处理等内容。第 13 ~ 15 章为实战部分,以实际生活中的数学问题为例,将前文介绍的各类科学计算算法应用其中。本书内容全面、通俗易懂,适合有一定 MATLAB 基础、想要进行进阶学习的读者。
5 \$ W: D, D' _5 P' m! V) t$ s了解更多, x' V4 R( D7 {( e! ~
▼▼▼
; N6 R2 |& ]* _9 ]- Z4 C2 O
agkdwtlf2i164027199455.jpg
" n1 X6 Q% c& J5 }" o+ M% J
. G) O( g- b2 I" W7 y
【抽奖方式及满足条件】:
! i( [0 |" ^" W9 A$ G1.关注「好玩的MATLAB 」公众号和视频号" z( G2 K$ j; D- l% d
eh0af5ane1h64027199555.jpg
: L8 _6 R0 b1 f5 ]6 }5 T2.给本文点【赞】+【在看】;
3 c( a L' M: `# L3.留言区评论点赞最多的前3名。
; P' y% A$ g2 D$ ^* w- h8 x% N4.本活动只针对从未获过奖的同学,之前获过奖的小伙伴,不用参加。& K) o; t, n( x$ E" P) w
同时满足上述4个条件的读者朋友,包邮赠送一本
# V; \' ~" g5 _, S( e- k【开奖时间】:2023年10月10日夜晚8点8 x9 C6 ]% T, l/ i+ y" x
【领奖方式】:在开奖时加小编私人微信:idmatlab
5 u# F1 ?5 u. b" C/ z$ ]. p扫一扫加管理员微信
- W4 v: W% O' \, h& q8 T8 c$ ]+ k
geyujacsgso64027199655.png
4 W$ O$ m3 s" [
x5aun0apggs64027199756.jpg
|
|