Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2001
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2001

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: Labeled TreePlot

  • To: mathgroup at smc.vnet.net
  • Subject: [mg31086] RE: [mg31037] Labeled TreePlot
  • From: "David Park" <djmp at earthlink.net>
  • Date: Tue, 9 Oct 2001 01:55:37 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

The notebook expression at the bottom of the email shows how to label a
TreePlot, or in the actual example an ExprPlot.

> From: Mark S. Coleman [mailto:mcoleman at bondspace.com]
To: mathgroup at smc.vnet.net
>
> Greetings,
>
> I am searching for a way to generate a "labeled Tree Plot", i.e.,
> placing a text label at each node of a tree. There does not appear a
> way to do this using the TreePlot function.
>
> Thanks!
>

Notebook[{

Cell[CellGroupData[{
Cell["TreePlots", "Title"],

Cell["\<\
David Park
djmp at earthlink.net\
\>", "Subtitle"],

Cell[BoxData[{
    \(Needs["\<DiscreteMath`Tree`\>"]\), "\[IndentingNewLine]",
    \(Needs["\<Graphics`Colors`\>"]\)}], "Input"],

Cell[CellGroupData[{

Cell["Labeling an expression tree ", "Section"],

Cell[BoxData[
    \(\(expr =
        f[g[x, y, z], g[x, y, h[x, y]], g[x, y]];\)\)], "Input"],

Cell["\<\
Here we make the basic TreePlot with all the Line elements extracted.\
\
\>", "Text"],

Cell[CellGroupData[{

Cell[BoxData[
    \(exprtree = ExprPlot[expr] // First\)], "Input"],

Cell[BoxData[
    \({Line[{{\(-2\), 0}, {2, 0}}], Line[{{\(-2\), 0}, {\(-2\), 1}}],
      Line[{{0.`, 0}, {0.`, 1}}], Line[{{2.`, 0}, {2.`, 1}}],
      Line[{{\(-2.317460317460317`\), 1}, {\(-1.6825396825396826`\),
            1}}], Line[{{\(-2.317460317460317`\),
            1}, {\(-2.317460317460317`\), 2}}],
      Line[{{\(-2.`\), 1}, {\(-2.`\), 2}}],
      Line[{{\(-1.6825396825396826`\), 1}, {\(-1.6825396825396826`\),
            2}}], Line[{{\(-0.31746031746031744`\),
            1}, {0.31746031746031744`, 1}}],
      Line[{{\(-0.31746031746031744`\),
            1}, {\(-0.31746031746031744`\), 2}}],
      Line[{{0.`, 1}, {0.`, 2}}],
      Line[{{0.31746031746031744`, 1}, {0.31746031746031744`, 2}}],
      Line[{{0.16628873771730915`, 2}, {0.46863189720332576`, 2}}],
      Line[{{0.16628873771730915`, 2}, {0.16628873771730915`, 3}}],
      Line[{{0.46863189720332576`, 2}, {0.46863189720332576`, 3}}],
      Line[{{1.6825396825396826`, 1}, {2.317460317460317`, 1}}],
      Line[{{1.6825396825396826`, 1}, {1.6825396825396826`, 2}}],
      Line[{{2.317460317460317`, 1}, {2.317460317460317`,
            2}}]}\)], "Output"]
}, Open  ]],

Cell["\<\
The nodes are the horizontal lines, and the node point is the \
midpoint of the line. So this extracts the nodes.\
\>", "Text"],

Cell[CellGroupData[{

Cell[BoxData[
    \(nodes =
      Cases[exprtree,
        Line[{{x1_, a_}, {x2_, a_}}] \[Rule] {\((x1 + x2)\)/2,
            a}]\)], "Input"],

Cell[BoxData[
    \({{0, 0}, {\(-2.`\), 1}, {0.`, 1}, {0.31746031746031744`,
        2}, {2.`, 1}}\)], "Output"]
}, Open  ]],

Cell["This groups each node with an index number.", "Text"],

Cell[CellGroupData[{

Cell[BoxData[
    \(nodelabels = MapIndexed[List, nodes]\)], "Input"],

Cell[BoxData[
    \({{{0, 0}, {1}}, {{\(-2.`\), 1}, {2}}, {{0.`,
          1}, {3}}, {{0.31746031746031744`, 2}, {4}}, {{2.`,
          1}, {5}}}\)], "Output"]
}, Open  ]],

Cell["This plots the tree with the node labels. ", "Text"],

Cell[BoxData[
    \(\(Show[
        Graphics[\[IndentingNewLine]{exprtree, \
\[IndentingNewLine]\(Text[#2\[LeftDoubleBracket]1\[RightDoubleBracket]\
, #1, Background \[Rule] LightPink] &\) @@@
              nodelabels}], \[IndentingNewLine]AspectRatio \[Rule]
          Automatic, \[IndentingNewLine]Frame \[Rule]
          True, \[IndentingNewLine]ImageSize \[Rule]
          500];\)\)], "Input"],

Cell["This is a list of our desired node names.", "Text"],

Cell[BoxData[
    \(\(nodenames = {f, g, g, h, g};\)\)], "Input"],

Cell["This now labels with the node names.", "Text"],

Cell[BoxData[
    \(\(Show[
        Graphics[\[IndentingNewLine]{exprtree, \
\[IndentingNewLine]\(Text[
                  nodenames\[LeftDoubleBracket]#2\[LeftDoubleBracket]\
1\[RightDoubleBracket]\[RightDoubleBracket], #1,
                  Background \[Rule] LightPink] &\) @@@
              nodelabels}], \[IndentingNewLine]AspectRatio \[Rule]
          Automatic, \[IndentingNewLine]Frame \[Rule]
          True, \[IndentingNewLine]ImageSize \[Rule]
          500];\)\)], "Input"],

Cell["\<\
The leaves are the terminal points of vertical lines that aren't node \
points.\
\>", "Text"],

Cell[CellGroupData[{

Cell[BoxData[{
    \(\(leaves =
        Cases[exprtree,
          Line[{{a_, x1_}, {a_, x2_}}] \[Rule] {a,
              x2}];\)\), "\[IndentingNewLine]",
    \(leaves = Complement[leaves // N, nodes // N]\)}], "Input"],

Cell[BoxData[
    \({{\(-2.317460317460317`\), 2.`}, {\(-2.`\),
        2.`}, {\(-1.6825396825396826`\),
        2.`}, {\(-0.31746031746031744`\), 2.`}, {0.`,
        2.`}, {0.16628873771730915`, 3.`}, {0.46863189720332576`,
        3.`}, {1.6825396825396826`, 2.`}, {2.317460317460317`,
        2.`}}\)], "Output"]
}, Open  ]],

Cell[CellGroupData[{

Cell[BoxData[
    \(leaflabels = MapIndexed[List, leaves]\)], "Input"],

Cell[BoxData[
    \({{{\(-2.317460317460317`\), 2.`}, {1}}, {{\(-2.`\),
          2.`}, {2}}, {{\(-1.6825396825396826`\),
          2.`}, {3}}, {{\(-0.31746031746031744`\), 2.`}, {4}}, {{0.`,
          2.`}, {5}}, {{0.16628873771730915`,
          3.`}, {6}}, {{0.46863189720332576`,
          3.`}, {7}}, {{1.6825396825396826`,
          2.`}, {8}}, {{2.317460317460317`, 2.`}, {9}}}\)], "Output"]
}, Open  ]],

Cell[BoxData[
    \(\(Show[
        Graphics[\[IndentingNewLine]{exprtree, \
\[IndentingNewLine]\(Text[#2\[LeftDoubleBracket]1\[RightDoubleBracket]\
, #1, Background \[Rule] LightPink] &\) @@@
              nodelabels, \[IndentingNewLine]\(Text[#2\
\[LeftDoubleBracket]1\[RightDoubleBracket], #1,
                  Background \[Rule] LightBlue] &\) @@@
              leaflabels}], \[IndentingNewLine]AspectRatio \[Rule]
          Automatic, \[IndentingNewLine]Frame \[Rule]
          True, \[IndentingNewLine]ImageSize \[Rule]
          500];\)\)], "Input"],

Cell["Here is our list of leaf names.", "Text"],

Cell[BoxData[
    \(\(leafnames = {x, y, z, x, y, x, y, x, y};\)\)], "Input"],

Cell["And we get out final labeled tree.", "Text"],

Cell[BoxData[
    \(\(Show[
        Graphics[\[IndentingNewLine]{exprtree, \
\[IndentingNewLine]\(Text[
                  nodenames\[LeftDoubleBracket]#2\[LeftDoubleBracket]\
1\[RightDoubleBracket]\[RightDoubleBracket], #1,
                  Background \[Rule] LightPink] &\) @@@
              nodelabels, \[IndentingNewLine]\(Text[
                  leafnames\[LeftDoubleBracket]#2\[LeftDoubleBracket]\
1\[RightDoubleBracket]\[RightDoubleBracket], #1,
                  Background \[Rule] LightBlue] &\) @@@
              leaflabels}], \[IndentingNewLine]AspectRatio \[Rule]
          Automatic, \[IndentingNewLine]Frame \[Rule]
          False, \[IndentingNewLine]Background \[Rule]
          Linen, \[IndentingNewLine]ImageSize \[Rule]
          500];\)\)], "Input"]
}, Open  ]]
}, Open  ]]
},
FrontEndVersion->"4.1 for Microsoft Windows",
ScreenRectangle->{{0, 1280}, {0, 943}},
WindowSize->{622, 738},
WindowMargins->{{0, Automatic}, {Automatic, 0}},
ShowSelection->True
]



  • Prev by Date: Re: Animated GIF
  • Next by Date: Re: convert .EPS to .GIF or .JPEG
  • Previous by thread: Labeled TreePlot
  • Next by thread: Q: Is there an "inverse" of ToRules?