MATLAB | 如何绘制这样的描边散点图?

part.-1 前前言

最近略忙可能更新的内容会比较简单,见谅哇,今日更新内容:


part.0 前言

看到gzhBYtools科研笔记(推荐大家可以去瞅瞅,有很多有意思的图形的R语言复现!!)做了这样一张图:

感觉很有意思,但是作者是使用 R语言+Adobe illustrator 进行完成的,于是俺就想试试能不能用 MATLAB来进行完成。该图是出自论文:

  • Li, Pu-Dong & Zhu, Zeng-Rong & Zhang, Yunzeng & Xu, Jianping & Wang, Hongkai & Wang, Zhengyi & Li, Hongye. (2022). The phyllosphere microbiome shifts toward combating melanose pathogen. Microbiome. 10. 10.1186/s40168-022-01234-x.

其中的Figure.6的图A:

想要下载文章可以通过将以下链接复制到浏览器打开来实现:

当然我的文末所给出的Gitee仓库也给出了pdf文件,可以下载下来看。


part.1 核心函数

就是将scatter函数画两遍散点,一遍有边缘,另一遍删掉边缘,同时设置其中一次绘图不在图例中显示即可,也就三五行代码的一个小小小函数,可以新建m文件(Matlab-file)保存一下,或者直接放在代码最后即可。

matlab 复制代码
function stHdl = strokeScatter(varargin)
    set(gca,'NextPlot','add');
    scHdl = scatter(varargin{:});
    stHdl = scatter(varargin{:},'MarkerEdgeColor','none');
    scHdl.Annotation.LegendInformation.IconDisplayStyle='off';
end

part.2 使用及复刻

基本使用

使用数据(所用数据见文末Gitee仓库)直接循环绘图就已经很像了:

matlab 复制代码
% BinningDemo
Date  = readtable('binningData.txt','Delimiter',',');
% Group = flipud(unique(Date.group))
Group = {'other bins'; 'bin 19'; 'bin 13'; 'bin 11'; 'bin 3'; 'bin 5'};
CList = [177,177,176; 63,168,106; 24,222,154;
         96,0,123; 189,83,110; 170,219,87]./255;

figure('Units','normalized','Position',[.2,.3,.4,.6]);
% 循环绘制散点
for i = 1:length(Group)
    ind  = strcmp(Date.group,Group(i));
    binX = Date.X(ind);
    binY = Date.Y(ind);
    if i == 1
        strokeScatter(binX,binY,170,'filled','CData',CList(i,:),...
            'MarkerEdgeColor','none');
    else
        strokeScatter(binX,binY,170,'filled','CData',CList(i,:),...
            'MarkerEdgeColor','k','LineWidth',2);
    end
end

坐标区域修饰

matlab 复制代码
% 坐标区域修饰
ax = gca;
ax.PlotBoxAspectRatio = [1,1,1];
ax.Box = 'on';
ax.LineWidth = 2;
ax.FontName = 'Times New Roman';
ax.XGrid = 'on';
ax.YGrid = 'on';
ax.TickDir = 'out';
ax.XLim = [0.2,0.85];
ax.YTick = 0:50:150;
ax.XTick = 0.4:0.2:0.8;
ax.FontSize = 14;
ax.XColor = [.2,.2,.2];
ax.YColor = [.2,.2,.2];
% X轴副标签
ax.XRuler.SecondaryLabel.String = 'GC content';
ax.XRuler.SecondaryLabel.Position(1) = ax.XLim(1);
ax.XRuler.SecondaryLabel.HorizontalAlignment = 'left';
ax.XRuler.SecondaryLabel.FontSize = 16;
ax.XRuler.SecondaryLabel.VerticalAlignment = 'bottom';
ax.XRuler.SecondaryLabel.FontWeight = 'bold';
% Y轴标签
ax.YLabel.String = 'Contig abundance';
ax.YLabel.FontSize = 24;
ax.YLabel.FontWeight = 'bold';

添加图例

matlab 复制代码
% 绘制图例
lgdHdl = legend(Group);
lgdHdl.NumColumns = length(Group);
lgdHdl.Location = 'southoutside';
lgdHdl.Box = 'off';
lgdHdl.FontSize = 14;

part.3 其他示例

这里随便写了点代码,懒得去想咋生成一团一团数据,干脆同时让X坐标取了个整,让点聚在了一起:

matlab 复制代码
clc; clear; close all
rng(6)
% 生成随机点(Generate random points)
mu = [1 1; 12 10; 9 12];
S  = cat(3,[1 0; 0 2],[1 0; 0 2],[1 0; 0 1]);
r1 = abs(mvnrnd(mu(1,:),S(:,:,1),200));
r2 = abs(mvnrnd(mu(2,:),S(:,:,2),200));
r3 = abs(mvnrnd(mu(3,:),S(:,:,3),200));
% 绘制散点图(Draw scatter chart)
hold on
propCell = {'LineWidth',2,'MarkerEdgeColor',[.3,.3,.3],'SizeData',100};
strokeScatter(round(r1(:,1)),r1(:,2),'filled','CData',[0.40 0.76 0.60],propCell{:});
strokeScatter(round(r2(:,1)),r2(:,2),'filled','CData',[0.99 0.55 0.38],propCell{:});
strokeScatter(round(r3(:,1)),r3(:,2),'filled','CData',[0.55 0.63 0.80],propCell{:});
% 增添图例(Draw legend)
lgd = legend('class1','class2','class3');
lgd.Location = 'northwest';
lgd.FontSize = 14;
% 坐标区域基础修饰(Axes basic decoration)
ax=gca; grid on
ax.FontName   = 'Cambria';
ax.Color      = [0.9,0.9,0.9];
ax.Box        = 'off';
ax.TickDir    = 'out';
ax.GridColor  = [1 1 1];
ax.GridAlpha  = 1;
ax.LineWidth  = 1;
ax.XColor     = [0.2,0.2,0.2];
ax.YColor     = [0.2,0.2,0.2];
ax.TickLength = [0.015 0.025];
% 隐藏轴线(Hide XY-Ruler)
pause(1e-6)
ax.XRuler.Axle.LineStyle = 'none';
ax.YRuler.Axle.LineStyle = 'none';


function stHdl = strokeScatter(varargin)
    set(gca,'NextPlot','add');
    scHdl = scatter(varargin{:});
    stHdl = scatter(varargin{:},'MarkerEdgeColor','none');
    scHdl.Annotation.LegendInformation.IconDisplayStyle='off';
end

再次推荐一下gzhBYtools科研笔记,虽然账号似乎建立时间不长但是有很多优质内容的!

本文所有代码及论文原文可在以下Gitee仓库获取:

https://gitee.com/slandarer/spdraw

相关推荐
我爱C编程2 小时前
基于拓扑结构检测的LDPC稀疏校验矩阵高阶环检测算法matlab仿真
算法·matlab·矩阵·ldpc·环检测
Virgil1394 小时前
数学建模练习题——多元统计分析
数学建模
通信射频老兵4 小时前
卫星通信基础知识---自由空间衰减和天线增益计算
经验分享·5g·数学建模·信号处理·射频工程
hie9889418 小时前
MATLAB锂离子电池伪二维(P2D)模型实现
人工智能·算法·matlab
88号技师1 天前
2025年6月一区-田忌赛马优化算法Tianji’s horse racing optimization-附Matlab免费代码
开发语言·算法·matlab·优化算法
m0_555762901 天前
matlab 教程
matlab
松果集1 天前
MATLAB基础知识【8】绘图,peaks,sphere,积分
matlab
wwer1425263631 天前
数学建模_时间序列
数学建模
wwer1425263632 天前
数学建模_拟合
数学建模
FF-Studio2 天前
【硬核数学 · LLM篇】3.1 Transformer之心:自注意力机制的线性代数解构《从零构建机器学习、深度学习到LLM的数学认知》
人工智能·pytorch·深度学习·线性代数·机器学习·数学建模·transformer