MATLAB | How to draw such a stroked scatterplot?

part.-1 Preface

I'm a little busy recently, maybe the updated content will be relatively simple, sorry, wow, today's updated content:


part.0 Preface

Seeing gzh BYtools科研笔记(recommend everyone to take a look, there are many interesting graphs reproduced in R language!!) made such a picture:

It feels very interesting, but the author used R language + Adobe illustrator to complete it, so I wanted to see if I could use MATLAB to complete it. The figure is from the paper:

  • 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 A of Figure.6:

To download an article, you can open it by copying the link below into your browser:

  • https://www.researchgate.net/journal/Microbiome-2049-2618/publication/359692392_The_phyllosphere_microbiome_shifts_toward_combating_melanose_pathogen/links/6248fec18068956f3c6532fa/The-phyllosphere-microbiome-shifts-toward-combating-melanose-pathogen.pdf

Of course, the Gitee warehouse given at the end of my article also gives a pdf file, which can be downloaded and viewed.


part.1 core function

It is to draw scatterthe scatter points twice with the function, one with edges, and the other to delete the edges, and at the same time set one of the plots not to be displayed in the legend. It is just a small function of three or five lines of code, which can create a new m file (Matlab- file) or just put it at the end of the code.

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

part.2 Use and reproduction

basic use

The use of data (see the Gitee warehouse at the end of the article for the data used) directly loops and draws is very similar:

% 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

Coordinate area modification

% 坐标区域修饰
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';

add legend

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


part.3 Other examples

I just wrote some code here, too lazy to think about how to generate a bunch of data, so I simply let the X coordinates be rounded at the same time, so that the points are gathered together:

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


over

Recommend gzh again BYtools科研笔记, although the account seems to have not been established for a long time, but there are a lot of high-quality content!

All codes and original papers in this article are available in the following Gitee warehouses:

https://gitee.com/slandarer/spdraw

Guess you like

Origin blog.csdn.net/slandarer/article/details/132013578