gpt4 book ai didi

graph - TreeForm 无重叠

转载 作者:行者123 更新时间:2023-12-04 12:00:40 24 4
gpt4 key购买 nike

由于重叠,我有时会遇到 TreeForm 中的标签不可读的问题。下面是一个例子,任何人都可以看到摆脱重叠的方法吗?

{{4, 5, 6}, {{{2, 4, 5, 6}, {{{1, 2, 4}, {}}, {{2, 3, 6}, {}}}}, {{4,      5, 6, 8}, {{{4, 7, 8}, {}}, {{6, 8, 9}, {}}}}}} // TreeForm


(source: yaroslavvb.com)

Belisarius' solution helps with overlap, but loses Tooltips, ie compare with

TreeForm[Hold[  GraphPlotHighlight[edges : {((_ -> _) | {_ -> _, _}) ...},     hl : {___} : {}, opts : OptionsPattern[]] :=    Module[{verts, coords, g, sub}, 5]]]


(source: yaroslavvb.com)

Answer update 11/12I ended up using code below (belisarius' code with a minor fix)

myTreeForm[exp_] := 
Module[{tooltipText, i},
tooltipText =
Cases[Cases[MakeBoxes[TreeForm@exp, StandardForm],
TooltipBox[x__] -> x, 7, Heads -> True],
TagBox[x__, y__] -> DisplayForm[First@{x}], Heads -> True];
i = 0;
TreeForm[exp,
VertexRenderingFunction -> ({Tooltip[
Inset[Rasterize[Text[" " <> ToString@#2 <> " "],
Background -> LightBlue], #1], tooltipText[[i++]]]} &)]];

最佳答案

我以前这样做过,但从未概括过结果。

    rectOffset = {.25,.1};
fontSize = 10
TreeForm[list,
VertexRenderingFunction -> ({White, EdgeForm[Black],
Rectangle[#1 - rectOffset, #1 + rectOffset], Black,
Text[ Style[#2, fontSize], #1]} &)]

alt text

使用工具提示编辑

使用“不同的方法”

代码很脏,抱歉现在没有时间清理它
rectOffset = {.33, .1};
fontSize = 9;
p = Cases[
Cases[MakeBoxes[TreeForm@list, StandardForm], TooltipBox[x__] -> x,
7, Heads -> True], TagBox[x__, y__] -> DisplayForm[First@{x}],
Heads -> True];
i = 0;
TreeForm[list,
VertexRenderingFunction -> ({White, EdgeForm[Black],
Rectangle[#1 - rectOffset, #1 + rectOffset], Black,
Tooltip[Text[Style[#2, fontSize], #1], p[[i++]]]} &)]

输出

alt text

编辑 2

我觉得这个版本更好:
Clear["Global`*"];
list = Hold[
GraphPlotHighlight[edges : {((_ -> _) | {_ -> _, _}) ...},
hl : {___} : {}, opts : OptionsPattern[]] :=
Module[{verts, coords, g, sub}, 5]];

myTreeForm[exp_] :=

Module[{ps, tooltipText, i},

ps[text_] := Rasterize[Text[Style[text]], "RasterSize"];

tooltipText =
Cases[Cases[MakeBoxes[TreeForm@list, StandardForm],
TooltipBox[x__] -> x, 7, Heads -> True],
TagBox[x__, y__] -> DisplayForm[First@{x}], Heads -> True];

i = 0;

TreeForm[list,
EdgeRenderingFunction -> ({Red, Line[#1]} &),
VertexRenderingFunction -> ({White, EdgeForm[Black], {}, Black,
Tooltip[
Inset[Rasterize[Text[" " <> ToString@#2 <> " "],
Background -> LightBlue], #1], tooltipText[[i++]]]} &)]
];

list // myTreeForm

输出:

alt text

编辑 4 ... 和最后一个

清理代码,删除那些只是为了使事情复杂化的虚假函数和变量:
myTreeForm[list_] := Module[{tooltipText, i},  

tooltipText =
Cases[Cases[MakeBoxes[TreeForm@list, StandardForm],
TooltipBox[x__] -> x, 7, Heads -> True],
TagBox[x__, y__] -> DisplayForm[First@{x}], Heads -> True];
i = 0;
TreeForm[list,
VertexRenderingFunction ->
({Tooltip[Inset[Rasterize[Text[" " <> ToString@#2 <> " "],
Background -> LightBlue], #1], tooltipText[[i++]]]} &)
]
];

哼!

关于graph - TreeForm 无重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4136844/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com