本文介绍了如何在MATLAB中绘制一棵树使其边缘处于直角?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 treeplot ,我得到了这个结果:

  nodes = [ 0 1 2 2 4 4 4 1 8 8 10 10]; 
treeplot(nodes)


我如何绘制树状树状图(带直边/分支)?就像下面这张图一样,我用Python的绘制了,但不是同一棵树,但它只是为了展示我想要的可视化类型:





当我检查文件 treeplot ,它表示:

  treeplot(P,nodeSpec,edgeSpec)允许可选参数nodeSpec 
和edgeSpec来设置节点或边缘颜色,标记和线条样式。
使用''省略一个或两个。

但是它没有提供任何让直线树代替默认的角一个。

解决方案

这是一个小问题,因为行句柄不是由


With treeplot, I get this:

nodes = [0 1 2 2 4 4 4 1 8 8 10 10];
treeplot(nodes)

How can I draw a tree like a dendrogram (with straight edges/branches)? Like this figure below, which I drew the following with Python's plotly, not of the same tree though, but its just to demonstrate the kind of visualization I want:

When I check the doc for treeplot, it says:

treeplot(P,nodeSpec,edgeSpec) allows optional parameters nodeSpec
and edgeSpec to set the node or edge color, marker, and linestyle.
Use '' to omit one or both.

But it does not state any option for making a straight-edged tree instead of the default "angular" one.

解决方案

It's a little bit of a kluge to do, since the line handles aren't returned by treeplot, and the line objects aren't tagged to easily find them. But if it's the only thing you've plotted in the current axes then the following should find the right line objects and modify them accordingly:

treeplot(nodes);                              % Plot tree
hLines = get(gca, 'Children');                % Get handles to children of axes
x = reshape(get(hLines(1), 'XData'), 3, []);  % Get and reshape x data
y = reshape(get(hLines(1), 'YData'), 3, []);  % Get and reshape y data
x = x([1 1 2 3], :);                          % Replicate first row of x
y = y([1 2 2 3], :);                          % Replicate second row of y
set(hLines(1), 'XData', x(:).', 'YData', y(:).');  % Reshape and update data

And here's the result:

这篇关于如何在MATLAB中绘制一棵树使其边缘处于直角?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 10:55