MATLAB | MATLAB弦图绘制工具又有了两种标刻度的格式?

文摘   2024-08-09 19:56   英国  
请尊重原创劳动成果
转载请注明本文链接
及文章作者:slandarer

前言

Hey,大噶好,最近真的是好久不见,今天带来的是对弦图绘制函数的更新,新增了两种标刻度线的方法,在此之间首先介绍一下本人编写的弦图绘制工具函数:

本人编写的MATLAB弦图绘制函数已经可以满足大部分需求的绘制较为专业的弦图绘制,工具下载地址:

gitee链接

  • 弦图+有向弦图https://gitee.com/slandarer/matlab-chord-chart

fileexchange链接

  • chord chart 弦图 https://www.mathworks.com/matlabcentral/fileexchange/116550-chord-chart
  • Digraph chord chart 有向弦图https://www.mathworks.com/matlabcentral/fileexchange/121043-digraph-chord-chart

这俩函数的基础使用教程请见:

正文

我们在绘制弦图的时候,可能会遇到刻度标签挤在一起看不清的情况:

dataMat=randi([0,5],[8,8]);

CList=[75,146,241;252,180,65;224,64,10;5,100,146;191,191,191;
    26,59,105;255,227,130;18,156,221;202,107,75;0,92,219;
    243,210,136;80,99,129;241,185,168;224,131,10;120,147,190]./255;

figure('Units','normalized','Position',[.02,.05,.6,.85])
% TickMode 'value'(default)/'linear'/auto
BCC=biChordChart(dataMat,'Arrow','on','CData',CList);
BCC=BCC.draw();

% 添加刻度
BCC.tickState('on')
BCC.tickLabelState('on')

于是我新增了两种刻度标志的方法,分别是 linearauto


linear方法

linear 会在弦图外侧均匀的标上刻度,此外有两个属性是只有设置TickMode属性为 linear 才会生效的:

  • linearTickCompactDegree刻度的紧密程度,数值越高刻度线数量越多
  • linearMinorTick是否开启次刻度线

弦图例子

dataMat = randi([0,5], [14,3]);
colName = compose('T%d'1:3);
rowName = compose('F%d'1:14);

figure('Units','normalized''Position',[.02,.05,.6,.85])
% TickMode 'value'(default)/'linear'/auto
CC = chordChart(dataMat, 'RowName',rowName, 'ColName',colName, 'TickMode','linear');

% 刻度的设置要在draw()之前
% the setting of tick should before draw()
% 刻度的紧密程度,数值越高刻度线数量越多
% The compact degree of ticks, The higher the value, the more scales there are
CC.linearTickCompactDegree = 1;
% 是否开启次刻度线
% Minor ticks 'on'/'off'
CC.linearMinorTick = 'on';

CC = CC.draw();
CC.setFont('FontSize',17'FontName','Cambria')

% 显示刻度和数值
% Displays scales and numeric values
CC.tickState('on')
CC.tickLabelState('on')
CC.setTickFont('FontName','Cambria')

% 调节标签半径
% Adjustable Label radius
CC.setLabelRadius(1.3);

有向弦图例子

dataMat=randi([0,5],[8,8]);

CList=[75,146,241;252,180,65;224,64,10;5,100,146;191,191,191;
    26,59,105;255,227,130;18,156,221;202,107,75;0,92,219;
    243,210,136;80,99,129;241,185,168;224,131,10;120,147,190]./255;

figure('Units','normalized','Position',[.02,.05,.6,.85])
% TickMode 'value'(default)/'linear'/auto
BCC=biChordChart(dataMat,'Arrow','on','CData',CList,'TickMode','Linear');

% 刻度的设置要在draw()之前
% the setting of tick should before draw()
% 刻度的紧密程度,数值越高刻度线数量越多
% The compact degree of ticks, The higher the value, the more scales there are
BCC.linearTickCompactDegree = 2;
% 是否开启次刻度线
% Minor ticks 'on'/'off'
BCC.linearMinorTick = 'on';


BCC=BCC.draw();

% 添加刻度
BCC.tickState('on')
BCC.tickLabelState('on')

auto 方法

这个方法就很简单了,就是如果有两个刻度线离得太近,就会自动的将末端互相远离:

弦图例子

dataMat=randi([0,5],[8,8]);

CList=[75,146,241;252,180,65;224,64,10;5,100,146;191,191,191;
    26,59,105;255,227,130;18,156,221;202,107,75;0,92,219;
    243,210,136;80,99,129;241,185,168;224,131,10;120,147,190]./255;

figure('Units','normalized','Position',[.02,.05,.6,.85])
% TickMode 'value'(default)/'linear'/auto
BCC=biChordChart(dataMat,'Arrow','on','CData',CList,'TickMode','auto');

BCC=BCC.draw();

% 添加刻度
BCC.tickState('on')
BCC.tickLabelState('on')

有向弦图例子

dataMat=randi([0,5],[8,8]);

CList=[75,146,241;252,180,65;224,64,10;5,100,146;191,191,191;
    26,59,105;255,227,130;18,156,221;202,107,75;0,92,219;
    243,210,136;80,99,129;241,185,168;224,131,10;120,147,190]./255;

figure('Units','normalized','Position',[.02,.05,.6,.85])
% TickMode 'value'(default)/'linear'/auto
BCC=biChordChart(dataMat,'Arrow','on','CData',CList,'TickMode','auto');

BCC=BCC.draw();

% 添加刻度
BCC.tickState('on')
BCC.tickLabelState('on')

更多例子

dataMat = randi([0,5], [11,5]);
dataMat(1:6,1) = 0;
dataMat([11,7],1) = [45,25];
dataMat([1,4,5,7],2) = [20,20,30,30];
dataMat(:,3) = 0;
dataMat(6,3) = 45;
dataMat(1:5,4) = 0;
dataMat([6,7],4) = [25,25];
dataMat([5,6,9],5) = [25,25,25];

colName = {'Fly''Beetle''Leaf''Soil''Waxberry'};
rowName = {'Bartomella''Bradyrhizobium''Dysgomonas''Enterococcus',...
           'Lactococcus''norank''others''Pseudomonas''uncultured',...
           'Vibrionimonas''Wolbachia'};

figure('Units','normalized''Position',[.02,.05,.6,.85])
CC = chordChart(dataMat, 'rowName',rowName, 'colName',colName, 'Sep',1/80'TickMode','auto');
CC = CC.draw();
% 修改上方方块颜色(Modify the color of the blocks above)
CListT = [0.7765 0.8118 0.52160.4431 0.4706 0.38430.5804 0.2275 0.4549;
          0.4471 0.4039 0.67450.0157 0      0     ];
for i = 1:size(dataMat, 2)
    CC.setSquareT_N(i'FaceColor',CListT(i,:))
end
% 修改下方方块颜色(Modify the color of the blocks below)
CListF = [0.5843 0.6863 0.78430.1098 0.1647 0.32550.0902 0.1608 0.5373;
          0.6314 0.7961 0.21180.0392 0.2078 0.10590.0157 0      0     ;
          0.8549 0.9294 0.87450.3882 0.3255 0.40780.5020 0.7216 0.3843;
          0.0902 0.1843 0.18040.8196 0.2314 0.0706];
for i = 1:size(dataMat, 1)
    CC.setSquareF_N(i'FaceColor',CListF(i,:))
end
% 修改弦颜色(Modify chord color)
for i = 1:size(dataMat, 1)
    for j = 1:size(dataMat, 2)
        CC.setChordMN(i,j'FaceColor',CListT(j,:), 'FaceAlpha',.5)
    end
end

CC.tickState('on')
CC.labelRotate('on')
CC.setFont('FontSize',17'FontName','Cambria')
CC.tickLabelState('on');


CC.labelRotate('off')
textHdl = findobj(gca,'Tag','ChordLabel');
for i = 1:length(textHdl)
    if textHdl(i).Position(2) < 0
        if abs(textHdl(i).Position(1)) > .7
            textHdl(i).Rotation = textHdl(i).Rotation + 45;
            textHdl(i).HorizontalAlignment = 'right';
            if textHdl(i).Rotation > 90
                textHdl(i).Rotation = textHdl(i).Rotation + 180;
                textHdl(i).HorizontalAlignment = 'left';
            end
        else
            textHdl(i).Rotation = textHdl(i).Rotation + 10;
            textHdl(i).HorizontalAlignment = 'right';
        end
    end
end

dataMat = randi([0,10], [5,5]);

CList1 = [0.70,0.59,0.67
    0.62,0.70,0.62
    0.81,0.75,0.62
    0.80,0.62,0.56
    0.62,0.65,0.65];
CList2 = [0.02,0.02,0.02
    0.59,0.26,0.33
    0.38,0.49,0.38
    0.03,0.05,0.03
    0.29,0.28,0.32];
CList = CList1;

NameList={'CHORD','CHART','MADE','BY','SLANDARER'};

figure('Units','normalized''Position',[.02,.05,.6,.85])
BCC = biChordChart(dataMat, 'Arrow','on''CData',CList, 'Sep',1/30'Label',NameList, 'LRadius',1.33'TickMode','auto');
BCC = BCC.draw();

% 修改弦颜色(Modify chord color)
for i = 1:size(dataMat, 1)
    for j = 1:size(dataMat, 2)
        BCC.setChordMN(i,j'FaceAlpha',.5)
    end
end
% 修改方块颜色(Modify the color of the blocks)
for i = 1:size(dataMat, 1)
    BCC.setSquareN(i'EdgeColor',[0,0,0], 'LineWidth',5)
end

% 添加刻度
BCC.tickState('on')
% 修改字体,字号及颜色
BCC.setFont('FontSize',17'FontWeight','bold')
BCC.tickLabelState('on')
BCC.setTickFont('FontSize',9)

dataMat = rand([9,9]);
dataMat(dataMat > .7) = 0;
dataMat(eye(9) == 1) = (rand([1,9])+.2).*3;

CList = [0.85,0.23,0.24
0.96,0.39,0.18
0.98,0.63,0.22
0.99,0.80,0.26
0.70,0.76,0.21
0.24,0.74,0.71
0.27,0.65,0.84
0.09,0.37,0.80
0.64,0.40,0.84];

figure('Units','normalized''Position',[.02,.05,.6,.85])
BCC = biChordChart(dataMat, 'Arrow','on''CData',CList,'TickMode','linear');
BCC.linearMinorTick = 'on';
BCC = BCC.draw();

% 添加刻度、刻度标签
BCC.tickState('on')
BCC.tickLabelState('on')


% 修改字体,字号及颜色
BCC.setFont('FontName','Cambria''FontSize',17)


% 修改弦颜色(Modify chord color)
for i = 1:size(dataMat, 1)
    for j = 1:size(dataMat, 2)
        if dataMat(i,j) > 0
            BCC.setChordMN(i,j'FaceAlpha',.7)
        end
    end
end
BCC.labelRotate('on')

工具下载

篇幅问题完整工具函数就不复制在本文中了,工具函数下载请见:

百度网盘

  • 将会发在留言区

gitee链接

  • 弦图+有向弦图https://gitee.com/slandarer/matlab-chord-chart

fileexchange链接

  • chord chart 弦图 https://www.mathworks.com/matlabcentral/fileexchange/116550-chord-chart
  • Digraph chord chart 有向弦图https://www.mathworks.com/matlabcentral/fileexchange/121043-digraph-chord-chart

其他项目推荐

  • https://www.mathworks.com/matlabcentral/fileexchange/165846-matlab-plot-cheat-sheet


slandarer随笔
slandarer个人公众号,目前主要更新MATLAB相关内容。
 最新文章