如何在 Python 中创建像下面的代码示例那样的径向集群?
我找到了几个关于如何创建这些精确层次结构的示例(至少我相信它们是),如下所示 stackoverflow.com/questions/2982929/ 效果很好,几乎可以执行我正在寻找的功能。
[编辑]这是 Paul 代码的简化版本,现在应该更容易有人帮助将其转化为径向簇而不是当前的簇形状
import scipy
import pylab
import scipy.cluster.hierarchy as sch
def fix_verts(ax, orient=1):
for coll in ax.collections:
for pth in coll.get_paths():
vert = pth.vertices
vert[1:3,orient] = scipy.average(vert[1:3,orient])
# Generate random features and distance matrix.
x = scipy.rand(40)
D = scipy.zeros([40,40])
for i in range(40):
for j in range(40):
D[i,j] = abs(x[i] - x[j])
fig = pylab.figure(figsize=(8,8))
# Compute and plot the dendrogram.
ax2 = fig.add_axes([0.3,0.71,0.6,0.2])
Y = sch.linkage(D, method='single')
Z2 = sch.dendrogram(Y)
ax2.set_xticks([])
ax2.set_yticks([])
fix_verts(ax2,0)
fig.savefig('test.png')
但不是树 -像结构一样,我需要一个如下图所示的径向集群。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我相信您可以使用
networkx
包与matplotlib
结合使用来完成此操作。查看networkx
图库中的以下示例:http:// networkx.lanl.gov/examples/drawing/circular_tree.html
一般来说,
networkx
有许多非常好的图形分析和绘图方法I believe you can do this using the
networkx
package in conjunction withmatplotlib
. Check out the following example from thenetworkx
gallery:http://networkx.lanl.gov/examples/drawing/circular_tree.html
In general
networkx
has a number of really nice graph analysis and plotting methods我已经对这个问题进行了更多研究,现在似乎最好创建一个新函数,用于直接从
linkage
输出绘制radial cluster
(而不是破解绘制的一)。我最终可能会煮出一些东西,但不会很快。我假设您的数据自然接受这种径向嵌入。你验证过吗?
linkage
中是否存在适合您目的的方法?似乎任何方法
linkage
都会返回一个二叉树结构。在您的示例中,您有更通用的树。您需要一些额外的知识来合并树节点。一切准备就绪,破解原始树状图的想法就失效了。更新:
这个简单的示例图对于您的目的来说足够合理吗?如果是这样,我将能够发布一些非常简单的代码来实现它。
更新 2:
以下是代码:
radial_demo.py :
radial_grouper.py:
radial_support.py:
radial_visualizer.py:
可以找到源代码此处。请随意修改它,但请保持未来的修改与要点同步。
I have studied this issue a little bit more and it seems now to be best to create a new function for plotting
radial cluster
directly from thelinkage
output (rather than hacking the plotted one). I may cook up eventually something, but nothing very soon.I'm assuming that your data naturally admit this kind of radial embedding. Have you verified that? Does there exists a suitable method in the
linkage
for your purposes?It seems that for any method
linkage
will return a binary-tree structure. In your examples you have more general tree. You need some extra knowledge how to consolidate tree nodes. This all ready invalidates the idea of hacking the original dendrogram.Update:
Would this naive example plot be a reasonable similar enough for your purposes? If so, I'll be able to post some really simple code to achieve it.
Update 2:
Here is the code:
radial_demo.py:
radial_grouper.py:
radial_support.py:
radial_visualizer.py:
You can find the source code here. Please feel free to modify it anyway you like, but please keep the future modifications synced with the gist.
我添加了一个函数
fix_verts
来合并树状图中每个“U”底部的顶点。试试这个:
结果是这样的:
我希望这就是您所追求的。
I added a function
fix_verts
that merges the verticies at the base of each "U" in the dendrogram.try this:
The result is this:
I hope that is what you were after.
Vega 有一个 示例 非常类似于您的第一个图表。
您可以在他们的在线编辑器上使用它。超级酷且易于使用。
Vega has an example pretty much like your first diagram.
And you can play with it on their online editor. Super cool and easy to use.
这些径向树可以使用 Graphviz 创建。
通常,节点的位置在网络中并不重要。这就是为什么我们可以使用
D3.js
在任何可视化中拖动节点。尽管如此,节点的位置对于可视化很重要。我们需要在 NetworkX 中绘制网络时为节点分配位置。
这通常是通过在调用方法
nx.draw_networkx()
。可以使用nx.drawing.layout()
。可以使用
nx 创建径向树。 nx_agraph.graphviz_layout()
使用 Graphviz。您必须使用prog='twopi'
来代替prog='dot'
,径向布局。可执行代码块位于此处:
注意:您需要在环境中安装
graphviz
库。否则,graphviz_layout()
方法将不起作用。G
必须是一棵树。调用graphviz_layout()
方法时需要指定根节点。结果示例:
These radial trees can be created using Graphviz.
Ordinarily, the locations of the nodes are not important in a network. That's why we can drag the nodes around in any visualization using
D3.js
. Nonetheless, the locations of the nodes are important for visualization.We need to allocate positions to the nodes while plotting a network in
NetworkX
.This is usually achieved by passing the
pos
attribute while calling the methodnx.draw_networkx()
. Thepos
attribute (positions of the nodes) can be determined by using any of the layouts specified innx.drawing.layout()
.Radial trees can be created by using
nx.nx_agraph.graphviz_layout()
by using Graphviz. Instead ofprog='dot'
, you have to useprog='twopi'
for radial layout.The executable codeblock is here:
Note: You need to have the
graphviz
library installed in your environment. Else, thegraphviz_layout()
method won't work.G
must be a tree. You need to specify the root node while calling thegraphviz_layout()
method.Sample result:
最近,我创建了一个小型Python模块(https://github.com/koonimaru/radialtree)从 scipy 树状图输出中绘制圆形树状图。
以下是如何使用它的示例:
Recently, I have created a small Python module (https://github.com/koonimaru/radialtree) to draw a circular demdrogram from scipy dendrogram output.
Here is an example of how to use it: