电子产业一站式赋能平台

PCB联盟网

搜索
查看: 197|回复: 0
收起左侧

MATLAB|散点图|边际直方图

[复制链接]

239

主题

239

帖子

1507

积分

三级会员

Rank: 3Rank: 3

积分
1507
发表于 2023-10-9 21:26:00 | 显示全部楼层 |阅读模式
点击上方蓝字和“好玩的MATLAB”一起快乐玩耍吧!$ R. s/ R# O' Y& Y7 I' z

l4fcmpd0fkz6403021851.jpg

l4fcmpd0fkz6403021851.jpg
0 q: P  I) T9 v# }
好玩的matlab" ^* [' t; }# q* p& D
带你解锁不一样的matlab新玩法+ b- F& g9 V7 j& q0 q' V% b7 c$ I
9 a+ V" h% n0 T4 |
前面几篇推文详细的介绍了散点图和边际图的画法【MATLAB|回归曲线|置信区间|边际图|核密度填充图、MATLAB|聚类散点图|边际图|核密度填充图】,今天特意添加边际直方图的画法,喜欢此推文的小伙伴们记得点赞+关注+分享!【尊重作者劳动成果,转载请注明推文链接和公众号名】) ^) i0 j" D7 k% Y- s; i
效果效果图图
% A/ y& j' G7 {# D9 y* c/ l- c" D3 p* F边际直方图效果图
* f. L/ M, [( X* p( E0 ^8 ]# T* b

o1ua1p0mdfg6403021951.png

o1ua1p0mdfg6403021951.png

+ P3 T7 t+ K) d( D" C# C, F
, Q5 q) c7 V5 L: L) ~

y1mhxgwbuhl6403022051.gif

y1mhxgwbuhl6403022051.gif
7 b* W. M0 Q9 q& ~
- t. L! c% g' T. y4 c1 N
绘图工具函数
  • 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;! G& F( K# y6 D7 q2 t+ 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 属性
    9 U3 p/ Z: ?. c% q( D                % 检查 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';            end7 J4 s3 n" j$ K7 b
                % 这里绘制到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类,用于创建一个带有附加边际分布信息的直方图图形。以下是对该函数的分析和介绍:类属性:
    + g6 }0 Y: i; ~" y! [3 TXData:存储主图中的X轴数据。3 n, {8 l+ s* l; l
    YData:存储主图中的Y轴数据。
    5 T1 b7 V6 w; v# O+ Q. a! |: J' gFillColor:存储直方图的填充颜色。
      U2 t- [; P$ y# d# _! ]9 g
    FaceAlpha:存储填充区域的透明度。
    # o7 o& f' p# \2 W, X9 j0 A
    HFig:存储图形句柄。" U2 U. n( H6 z
    - }0 L' Z7 J& q" L, F6 O) O3 d

    fqdajandgd26403022151.png

    fqdajandgd26403022151.png
    1 c! e( \2 A0 v  L
    根据句柄的形式调用。
      Z$ ^0 v- S6 i. r- X构造函数 PlotDensHist(sHdl):
    $ ^; S8 ~3 v6 ]! U该构造函数接受一个图形句柄 sHdl 作为输入参数,并从该图形句柄中提取相关属性值,以初始化对象的属性。这些属性包括X轴数据、Y轴数据、填充颜色、线条颜色等。' W2 k, V4 H1 v) u/ f$ R* X
    构造函数将这些属性存储在对象的属性中,并设置默认的绘图参数。) T: F$ [1 r- Q
    plotDensHist 方法:
    ' T- g0 A0 ?0 R3 N3 w3 f5 @# lplotDensHist 方法用于在MATLAB图形中创建直方图,并添加附加的边际分布信息。
    ! a) }$ i1 F+ K3 h& v' q& ]; p该方法首先获取当前图形的句柄 hFig,然后创建主图、顶部区域和右侧区域的坐标轴位置。
    0 _7 F% G; ^1 _# L/ d主图的数据来自对象的属性 XData 和 YData,并根据对象的其他属性(如填充颜色、线条颜色等)来绘制直方图。/ y: ]. A# {5 }5 ?% g
    顶部和右侧区域分别用于绘制边际分布的直方图,使用的数据分别是 XData 和 YData。% i/ C" t; i. k9 A; p$ l
    方法还设置了坐标轴的各种属性,如刻度线样式、字体大小等。
    6 t$ J+ t) z5 n/ V9 w' u最后,通过 linkaxes 方法将主图和边际分布区域的坐标轴连接在一起,以确保它们在缩放时保持一致。" x0 P3 i5 w5 c
    [/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];
    " ]- P; H( H' H# [% 使用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');9 T6 w' K- f3 V  r7 q3 x4 P( R
    % 画出凸包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='';, C- x! Z& i/ U
    ax.GridLineStyle = '-.'; % 设置网格线样式为虚线ax.GridColor = 'k'; % 设置网格线颜色为红色ax.XGrid = 'off'; % 关闭X网格线ax.YGrid = 'off';  % 打开Y网格线) T7 j  _, o: [6 M; |
    ax.LineWidth = 1;            % 设置坐标线宽ax.XMinorTick = 'on';        % 打开x次要刻度线ax.YMinorTick = 'on';        % 打开y次要刻度线ax.TickDir = 'out';           % 设置刻度线方向向外ax.FontSize = 14;            % 设置坐标字体大小* Y/ ?3 `% n( p/ Z- l9 C" O
    Phdl1=PlotDensHist(sh1);Phdl1.plotDensHist();Phdl2=PlotDensHist(sh2);Phdl2.plotDensHist();Phdl3=PlotDensHist(sh3);Phdl3.plotDensHist();
      w9 W: l5 \8 Q* w

    1aldr2sbsuo6403022251.png

    1aldr2sbsuo6403022251.png
    / c% i6 \/ D$ M! u1 m
    - -THE END- -
    8 J- x4 U7 b) R& c: O  E5 D源码下载:gitee下载:https://gitee.com/iDMatlab/cool-scatter-chart
      c4 }/ z# }) z% Y

    mvishukw0fm6403022351.png

    mvishukw0fm6403022351.png
    " z/ `5 h1 u! x' \4 f! H
    参考资料:  @6 L, L: {- h1 T2 e8 b+ ~
    【1】MATLAB|炫酷的聚类散点图【2】MATLAB|聚类散点图|边际图|核密度填充图【3】MATLAB|回归曲线|置信区间|边际图|核密度填充图
    2 G8 D1 ~& C  A" |% i; x9 Q

    m4osbcuwiuw6403022451.gif

    m4osbcuwiuw6403022451.gif

    9 z* Y2 ]/ c" E1 e

    vzxpqeajbsh6403022551.png

    vzxpqeajbsh6403022551.png

    ) u: b  }: ^, A% ?. Y# f送书活动$ y2 E+ J5 T2 R

    ek2ujf3na426403022651.png

    ek2ujf3na426403022651.png
    / f2 [& r+ P0 c! e8 K

    la24b5c5dgp6403022751.gif

    la24b5c5dgp6403022751.gif
    7 I: J; z  ?9 L( ?- {
    6 T, @2 {) }4 r1 @
    包邮赠送 「北京大学出版社赞助《MATLAB科学计算从入门到精通》
    , j) Z' K' A9 Y9 L  Z( ^本书从 MATLAB 基础语法讲起,介绍了基于 MATLAB 函数的科学计算问题求解方法,实现了大量科学计算算法。
    $ a1 G- v# S- y$ v# U0 A! c" r本书分为三大部分。第 1 章和第 2 章为 MATLAB 的基础知识,对全书用到的 MATLAB 基础进行了简单介绍。第 3 ~ 12 章为本书的核心部分,包括线性方程组求解、非线性方程求解、数值优化、数据插值、数据拟合与回归分析、数值积分、常微分方程求解、偏微分方程求解、概率统计计算及图像处理与信号处理等内容。第 13 ~ 15 章为实战部分,以实际生活中的数学问题为例,将前文介绍的各类科学计算算法应用其中。本书内容全面、通俗易懂,适合有一定 MATLAB 基础、想要进行进阶学习的读者。8 h7 N( p+ T+ O; m0 \
    了解更多8 f) I  t4 E  M
    ▼▼▼& A& w7 j% l! h4 ?  U; Y# Q; z* [% n9 |7 X

    ezrzk1mstsd6403022852.jpg

    ezrzk1mstsd6403022852.jpg

    0 J# l7 Q/ N* W) A( z  ]) s% N$ [- ^$ ]- s. Q* }* M
    【抽奖方式及满足条件】:
    6 _; j1 J! S5 `1.关注「好玩的MATLAB 」公众号和视频号
    1 }: x7 x+ Q' l) ~3 }& M9 h' |# D0 V

    wioyc42imz16403022952.jpg

    wioyc42imz16403022952.jpg

      U& g4 [: W  E  P+ M2.给本文点【】+【在看】;
    2 R8 N9 M' @0 W; M5 r  @$ c3 k3.留言区评论点赞最多的前3名。5 h7 u: I0 l4 t0 s: t" }
    4.本活动只针对从未获过奖的同学,之前获过奖的小伙伴,不用参加。
    ( i2 E7 ?6 Q  U+ P$ Z9 ~同时满足上述4个条件的读者朋友,包邮赠送一本9 b4 \% j- s* [' D
    【开奖时间】:2023年10月10日夜晚8点  w4 |, h: P6 f. C
    【领奖方式】:在开奖时加小编私人微信:idmatlab
    4 f# i0 ?3 ~  `2 m5 a& y; _扫一扫加管理员微信
    9 U6 @  ?7 X+ f: I

    n43ey1o4wey6403023052.png

    n43ey1o4wey6403023052.png
    7 L  j: A+ u! u

    tc03pouxctw6403023152.jpg

    tc03pouxctw6403023152.jpg
  • 回复

    使用道具 举报

    发表回复

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则


    联系客服 关注微信 下载APP 返回顶部 返回列表