如何在 MATLAB 中绘制一棵树,使其边缘成直角?
How to draw a tree in MATLAB such that its edges are at right angles?
使用 treeplot
,我得到这个:
nodes = [0 1 2 2 4 4 4 1 8 8 10 10];
treeplot(nodes)
我怎样才能画出像树状图一样的树(直线edges/branches)?像下面这张图,是我和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.
但它没有说明制作直边树而不是默认 "angular" 树的任何选项。
这有点麻烦,因为 treeplot
, and the line objects aren't tagged 不返回行句柄以便轻松找到它们。但如果这是您在当前轴上绘制的唯一内容,那么以下内容应该找到正确的线对象并相应地修改它们:
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
结果如下:
使用 treeplot
,我得到这个:
nodes = [0 1 2 2 4 4 4 1 8 8 10 10];
treeplot(nodes)
我怎样才能画出像树状图一样的树(直线edges/branches)?像下面这张图,是我和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.
但它没有说明制作直边树而不是默认 "angular" 树的任何选项。
这有点麻烦,因为 treeplot
, and the line objects aren't tagged 不返回行句柄以便轻松找到它们。但如果这是您在当前轴上绘制的唯一内容,那么以下内容应该找到正确的线对象并相应地修改它们:
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
结果如下: