某些类型的对象在 Mathematica 中具有特殊的输入/输出格式。这包括 Graphics
、光栅图像,以及从 Mathematica 8 开始的图形 ( Graph[]
)。不幸的是,大型图形可能需要很长时间才能可视化,这比我在交互式工作期间对它们进行的大多数其他操作要长得多。
如何防止 Graph[]
的自动布局StandardForm 和 TraditionalForm 中的对象,并将它们显示为例如-Graph-
,最好保留输出的可解释性(可能使用 Interpretation
?)。我认为这将涉及更改 Format
和/或 MakeBoxes
以某种方式,但我没有成功地让它发挥作用。
我想以可逆的方式执行此操作,并且最好定义一个函数,该函数在应用于 Graph
时将返回原始交互式图形显示。对象(与 GraphPlot
不同,后者不是交互式的)。
在相关说明中,有没有办法检索与某些符号关联的 Format/MakeBoxes 定义? FormatValues
是一个相关函数,但对于 Graph
为空.
示例 session :
In[1]:= Graph[{1->2, 2->3, 3->1}]
Out[1]= -Graph-
In[2]:= interactiveGraphPlot[%] (* note that % works *)
Out[2]= (the usual interactive graph plot should be shown here)
最佳答案
虽然我没有 Mathematica 8 来尝试这个,但一种可能性是使用这个结构:
Unprotect[Graph]
MakeBoxes[g_Graph, StandardForm] /; TrueQ[$short] ^:=
ToBoxes@Interpretation[Skeleton["Graph"], g]
$short = True;
之后,一个
Graph
对象应以骨架形式显示,并设置 $short = False
应该恢复默认行为。希望这可以自动切换:
interactiveGraphPlot[g_Graph] := Block[{$short}, Print[g]]
Mark 对修改的担忧
Graph
让我考虑使用 $PrePrint
的选项.我认为这也应该防止发生缓慢的布局步骤。假设您还没有使用 $PrePrint
,这可能更可取。为了别的东西。$PrePrint =
If[TrueQ[$short], # /. _Graph -> Skeleton["Graph"], #] &;
$short = True
也很方便,至少有
Graphics
(同样,我无法在 v7 中使用 Graph
进行测试)您只需使用 Print
即可获得图形。 .在这里,用图形显示:g = Plot[Sin[x], {x, 0, 2 Pi}]
(* Out = <<"Graphics">> *)
然后
Print[g]
我离开了
$short
进行适当的测试,以便通过全局符号轻松切换,但可以省略它并使用: $PrePrint = # /. _Graph -> Skeleton["Graph"] &;
然后使用
$PrePrint = .
重置默认功能。
关于wolfram-mathematica - 防止在 Mathematica 8 中自动布局 Graph[] 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5938565/