如何在突出显示的图表中添加图例?

How to add legend in a highlighted graph?

我想根据不同的高亮边在图G中添加图例。是否可以只用一张图来完成G

这是一个可以玩的玩具示例。我有一个情节 G.

adj =[0 0 1 1 1;   % adjacency matrix
      1 0 1 0 1;
      0 1 0 1 1;
      1 1 1 0 1;
      0 0 1 0 0]
G = digraph(adj);

我根据边的类型用三种颜色高亮了所有的边。 3 种类型的边表示在我的例子中节点之间有 3 种不同的关系。

这就是我突出显示所有边缘的方式:

M(:,:,1)=[0 0 1 0 0;1 0 0 0 1;0 0 0 0 0;1 0 0 0 0;0 0 1 0 0];
M(:,:,2)=[0 0 0 1 0; 0 0 1 0 0;0 1 0 0 1;0 0 0 0 0;0 0 0 0 0];              
M(:,:,3)=[0 0 0 0 1; 0 0 0 0 0; 0 0 0 1 0;0 1 1 0 1;0 0 0 0 0];

我的问题的难点在于我必须删除出度小于某个整数(假设为 2)的顶点。因此我无法独立绘制 3 个图表。

rmvNode=find(outdegree(G)<2);    % outdegree is the reason why single G is neccesary
adj(rmvNode,:)=[]; adj(:,rmvNode)=[];
M(:,rmvNode,:)=[]; M(rmvNode,:,:)=[];
G=digraph(adj);

然后我们就可以绘制它了。

for k=1:3           %Looping depending on the third dimension
    [r,c]= find(M(:,:,k));  %Finding non-zero elements
    s{k}=r;     t{k}=c;    
end
h=plot(G);
highlight(h,s{1},t{1},'EdgeColor','r');
highlight(h,s{2},t{2},'EdgeColor','g');
highlight(h,s{3},t{3},'EdgeColor','b');

我的理想情况是这样的图例:将红色边缘分配给标签 'type 1',将蓝色边缘分配给 'type 2',将绿色边缘分配给 'type 3'。我想要这样的东西:

再说一遍:我无法根据M中的3页独立绘制3张图,将3张图组合在一起,然后添加图例。因为如你所见,outdegree 需要整个图 G 作为输入,所以将 G 划分为 G1G2 和 [=21= 是不可行的].

一种方法是像这样操纵 legend function by adding an invisible plot

%put this at the end of your code
hold on;                                      %to retain current plot
ax=plot(NaN,NaN,'r',NaN,NaN,'g',NaN,NaN,'b'); %plotting invisible points of desired colors
legend(ax,'Type 1','Type 2','Type 3');        %adding the legend

给出: