如何在MATLAB中绘制树,使其边缘成直角?

Kri*_*673 2 graphics tree matlab visualization graph

有了treeplot,我明白了:

nodes = [0 1 2 2 4 4 4 1 8 8 10 10];
treeplot(nodes)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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

在此输入图像描述

当我检查文档时treeplot,它说:

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.
Run Code Online (Sandbox Code Playgroud)

但它没有说明制作直边树而不是默认"角度"树的任何选项.

gno*_*ice 6

由于线条句柄没有返回treeplot,并且线条对象没有被标记以便轻易找到它们,所以这有点像一个块.但如果它是您在当前轴上绘制的唯一内容,则以下内容应找到正确的线对象并相应地修改它们:

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
Run Code Online (Sandbox Code Playgroud)

这是结果:

在此输入图像描述