绘制圆形节点网络,并在节点之间建立链接

发布于 2024-11-03 14:50:10 字数 794 浏览 13 评论 0 原文

我想绘制一个节点的圆形图,其中某些节点之间有链接。以下是社交网络图表中的一些示例:

example1
(来源:wrightresult.com

example2


(来源:twit88.com< /a>)

如何使用 MATLAB 完成此操作?是否可以不安装单独的软件包?

I would like to draw a circular graph of nodes where certain nodes have a link between them. Here are a few examples from social network graphs:

example1
(source: wrightresult.com)

example2

example3
(source: twit88.com)

How can this be done with MATLAB? Is it possible without installing a separate package?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感悟人生的甜 2024-11-10 14:50:10

这是您可以做您想做的事情的一种方法。首先,在圆上生成您感兴趣的点

clear;
theta=linspace(0,2*pi,31);theta=theta(1:end-1);
[x,y]=pol2cart(theta,1);

,然后,如果您知道连接的节点对,则可以跳过此步骤。但在许多情况下,您可以从其他计算中获得连接矩阵,并从中找到连接节点的索引。在这里,我创建了一个布尔连接矩阵。因此,如果有 N 个节点,则连接矩阵是一个 NxN 对称矩阵,其中第 i,j 个元素是1,这意味着您有从节点 i 到节点 j 的连接,否则为 0 。然后可以提取非零对的下标来获取节点连接(只需要上三角形)。

links=triu(round(rand(length(theta))));%# this is a random list of connections
[ind1,ind2]=ind2sub(size(links),find(links(:)));

这是我用上面的代码生成的连接矩阵。

在此处输入图像描述

现在我们只需绘制连接,一次一个,

h=figure(1);clf(h);
plot(x,y,'.k','markersize',20);hold on
arrayfun(@(p,q)line([x(p),x(q)],[y(p),y(q)]),ind1,ind2);
axis equal off

这将为您提供类似于您的图示例

在此处输入图像描述

Here is one way you can do what you want. First, generate points on the circle that you are interested in

clear;
theta=linspace(0,2*pi,31);theta=theta(1:end-1);
[x,y]=pol2cart(theta,1);

Next, if you know the pairs of nodes that are connected, you can skip this step. But in many cases, you get a connectivity matrix from other computations, and you find the indices of the connected nodes from that. Here, I've created a Boolean matrix of connections. So, if there are N nodes, the connectivity matrix is an NxN symmetric matrix, where if the i,jth element is 1, it means you have a connection from node i to node j and 0 otherwise. You can then extract the subscripts of the non-zero pairs to get node connections (only the upper triangle is needed).

links=triu(round(rand(length(theta))));%# this is a random list of connections
[ind1,ind2]=ind2sub(size(links),find(links(:)));

This is the connectivity matrix I generated with the code above.

enter image description here

Now we just need to plot the connections, one at a time

h=figure(1);clf(h);
plot(x,y,'.k','markersize',20);hold on
arrayfun(@(p,q)line([x(p),x(q)],[y(p),y(q)]),ind1,ind2);
axis equal off

which will give you a figure similar to your examples

enter image description here

七颜 2024-11-10 14:50:10

受到 Cleve 最新博文的启发莫勒,您还可以使用 gplot 函数在给定邻接矩阵和节点坐标的情况下绘制图形。

这是一个使用 < 的示例代码>巴基; MATLAB 的演示函数部分,用于生成截断二十面体的图形(看起来像足球)。在本例中,我们仅使用其邻接矩阵,因为我们将顶点布置为圆形:

%# 60-by-60 sparse adjacency matrix
A = bucky();
N = length(A);

%# x/y coordinates of nodes in a circular layout
r =  1;
theta = linspace(0,2*pi,N+1)'; theta(end) = [];
xy = r .* [cos(theta) sin(theta)];

%# labels of nodes
txt = cellstr(num2str((1:N)','%02d'));

%# show nodes and edges
line(xy(:,1), xy(:,2), 'LineStyle','none', ...
    'Marker','.', 'MarkerSize',15, 'Color','g')
hold on
gplot(A, xy, 'b-')
axis([-1 1 -1 1]); axis equal off
hold off

%# show node labels
h = text(xy(:,1).*1.05, xy(:,2).*1.05, txt, 'FontSize',8);
set(h, {'Rotation'},num2cell(theta*180/pi))

circular_graph


我们可以采用更进一步,尝试尽量减少边缘交叉。也就是说,我们要重新排列节点,使边缘尽可能接近圆的圆周。

这可以通过找到矩阵的对称排列来最小化其矩阵带宽(非零更接近对角线)

matrix_permutation

p = symrcm(A);
A = A(p,p);
txt = txt(p);

本例中的结果:

circular_graph_permutation

其他改进包括用曲线样条线替换直线来绘制边缘,(这样你就可以获得与第二个类似的更好的图形)你已经展示过),或者使用不同的颜色来显示顶点簇及其边缘(显然你需要进行图聚类)。我将把这些步骤留给你:)

Inspired by the latest blog post by Cleve Moler, you could also use the gplot function to draw a graph given an adjacency matrix and node coordinates.

Here is an example using bucky; a demo function part of MATLAB that generates the graph of a truncated icosahedron (looks like a soccer ball). We will only use its adjacency matrix for this example since we are laying out the vertices in a circular shape:

%# 60-by-60 sparse adjacency matrix
A = bucky();
N = length(A);

%# x/y coordinates of nodes in a circular layout
r =  1;
theta = linspace(0,2*pi,N+1)'; theta(end) = [];
xy = r .* [cos(theta) sin(theta)];

%# labels of nodes
txt = cellstr(num2str((1:N)','%02d'));

%# show nodes and edges
line(xy(:,1), xy(:,2), 'LineStyle','none', ...
    'Marker','.', 'MarkerSize',15, 'Color','g')
hold on
gplot(A, xy, 'b-')
axis([-1 1 -1 1]); axis equal off
hold off

%# show node labels
h = text(xy(:,1).*1.05, xy(:,2).*1.05, txt, 'FontSize',8);
set(h, {'Rotation'},num2cell(theta*180/pi))

circular_graph


We can take this a step further and try to minimize edge crossings. That is we want to rearrange the nodes so that the edges are as close as possible to the circumference of the circle.

This can be done by finding a symmetric permutation of the matrix that minimizes its bandwidth (non-zeros are closer to the diagonal)

matrix_permutation

p = symrcm(A);
A = A(p,p);
txt = txt(p);

The result in this case:

circular_graph_permutation

Other improvements include replacing straight lines with curved splines to draw the edges, (that way you get a nicer graph similar to the second one you've shown), or using different colors to show clusters of vertices and their edges (obviously you'll need to do graph clustering). I will leave those steps to you :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文