Matlab 如何在高亮显示的图形中添加图例?

Matlab 如何在高亮显示的图形中添加图例?,matlab,plot,graph,matlab-figure,legend,Matlab,Plot,Graph,Matlab Figure,Legend,我想根据不同的高亮显示边在图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种类型的边表示节点之间有3种不同的关系 以下是我高亮显示所有边的方式: M(:,:,1

我想根据不同的高亮显示边在图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种类型的边表示节点之间有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);
我的理想情况是这样一个图例:将红色边指定给标签“类型1”,将蓝色边指定给“类型2”,将绿色边指定给“类型3”。我想要这样的东西:

%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


再一次:我不能根据M中的3页单独绘制3个图形,将3个图形组合在一起,然后添加图例。因为正如您所看到的,
outdegree
需要一个完整的图形
G
作为输入,将
G
划分为
G1
G2
G3
一种方法是通过添加如下不可见项来操作函数:

%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
其中:


您可以使用它。顺便说一句,从代码的这些行中,
突出显示(h,s{2},t{2},'EdgeColor','g')突出显示(h,s{3},t{3},'EdgeColor','b')似乎您希望第2类为绿色,第3类为蓝色(我在回答中遵循了这一点),但在粗略的数字中,您交换了这些颜色。@SardarUsama感谢您指出这一点以及“不可见的绘图”的想法。明亮的不客气!最好使用
nan
s作为虚拟数据,因为它们是真正不可见的。