如何在 Matlab 绘图中显示较浅边缘的深色边缘?

How to display dark edges over lighter ones in Matlab plots?

我有以下代表图形的 Matlab 图。我想将较暗的显示在较浅的边缘之上,这样较浅的边缘在穿过它们时不会修改较暗的边缘。我怎么办?

编辑:重现示例的 Matlab 代码如下

plot(G, 'XData', Xcoords, 'YData', Ycoords,'NodeLabel',{}, 'MarkerSize', 7,...
 'Linewidth',1.6, 'EdgeCData', G.Edges.Weight)
  colormap(flipud(gray(40)));
  colorbar('southoutside');
  caxis([min(G.Edges.Weight) max(G.Edges.Weight)])
 axis off

边的权重在G.Edges.Weight

中编码

要重现效果(使用较小的图形),您可以尝试使用以下代码:

A= zeros(4,4);
A(1,[2 3 4])=1;
A(2,4)=0.04;
A(2,[1 3])=1;
A(3,[2 1 4])=1; 
A(4,2)=0.04;
A(4,[3 1])=1;

Xcoords=[1 2 2 1]';
Ycoords= [1 1 2 2 ]';

G= graph(A);% base toolbox

figure()
plot(G, 'XData', Xcoords, 'YData', Ycoords, 'NodeLabel',{}, 'MarkerSize', 7,...
    'LineWidth', 3.8, 'EdgeCdata', G.Edges.Weight)
colormap(flipud(gray(40)));
colorbar('southoutside'); caxis([0 1]);
axis off

似乎是边的顺序决定了谁在上面。例如,如果将权重 0.04 分配给另一条交叉边 (A(1,3)=A(3,1)),则效果不可见,因为边 A(2,4)=A(4,2)来了。

边 table 在 MATLAB 的 graph class 中的顺序似乎非常依赖于图的邻接矩阵中的位置,这本质上不可能以保证的方式设计一些任意的边缘顺序。所以我认为你只有两个选择:

  1. 编写你自己的绘图程序;然后您可以随意控制绘图顺序,因为这是您自己的软件设计。
  2. 使用 MATLAB 创建的未记录的基元处理 MATLAB 的图形绘制输出。

第二个选项是可能的,注意绘制的 GraphPlot 对象在其 NodeChildren 中有一个 LineStrip 对象,它负责绘制所有相关的边。因为您使用的是灰度颜色贴图,所以您只需要此对象中的 RGB 数据即可确定其顶点需要如何排序以获得正确的绘图顺序。

首先,将绘制的结果存储在P中,并将EdgeAlpha设置为1,从而绘制出图形

in such a way the lighter edges don't modify the darker when crossing them

P = plot(G, 'XData', Xcoords, 'YData', Ycoords, 'NodeLabel',{}, 'MarkerSize', 7,...
    'LineWidth', 3.8, 'EdgeCdata', G.Edges.Weight, 'EdgeAlpha',1);
colormap(flipud(gray(40)));
colorbar('southoutside'); caxis([0 1]);
axis off

然后找到绘图过程中创建的LineStrip

drawnow
s = P.NodeChildren(arrayfun(@(o) isa(o,'matlab.graphics.primitive.world.LineStrip'), P.NodeChildren));

然后可以根据 ColorData 确定 s 中顶点的新顺序,然后必须将其应用于 ColorDataVertexData 属性以在不改变任何其他内容的情况下重新排序边缘:

[~,idx] = sortrows(s.ColorData','desc');
set(s, 'VertexData',s.VertexData(:,idx),  'ColorData',s.ColorData(:,idx));

这可能会被发生的任何进一步重绘所覆盖,并且作为未记录的功能,无法保证其行为方式 - 但从表面上看,它似乎可以满足您的需求。