({{, (RefLink[TreePlot, paclet:ref/TreePlot])[g]
generates a tree plot of the graph g.}, {, (RefLink[TreePlot, paclet:ref/TreePlot])[{(Subscript[e, 1]),(Subscript[e, 2]),…}]
generates a tree plot of the graph with edges (Subscript[e, j]).}, {, (RefLink[TreePlot, paclet:ref/TreePlot])[{…,w[(Subscript[e, i])],…}]
plots (Subscript[e, i]) with features defined by the symbolic wrapper w.}, {, (RefLink[TreePlot, paclet:ref/TreePlot])[{(Subscript[v, i1])->(Subscript[v, j1]),…}]
uses rules (Subscript[v, i1])->(Subscript[v, j1]) to specify the graph g.}, {, (RefLink[TreePlot, paclet:ref/TreePlot])[m]
generates a tree plot of the graph represented by the adjacency matrix m.}, {, (RefLink[TreePlot, paclet:ref/TreePlot])[…,pos]
places roots of trees in the plot at position pos.}, {, (RefLink[TreePlot, paclet:ref/TreePlot])[…,…,v]
uses vertex v as the root vertex in the tree plot.}})

OpenersNotesSection(Spacer1)Details and Options

(RefLink[TreePlot, paclet:ref/TreePlot]) attempts to place vertices in a tree of successive layers, or a collection of trees.

(RefLink[TreePlot, paclet:ref/TreePlot]) supports the same vertices and edges as (RefLink[Graph, paclet:ref/Graph]).

If the graph g is not a tree, (RefLink[TreePlot, paclet:ref/TreePlot]) lays out its vertices on the basis of a spanning tree of each connected component of the graph.

(RefLink[TreePlot, paclet:ref/TreePlot])[g] attempts to choose the root so as to make trees have as few layers as possible.

(RefLink[TreePlot, paclet:ref/TreePlot])[g,pos] places the roots at position pos. Possible positions are: (RefLink[Top, paclet:ref/Top]), (RefLink[Bottom, paclet:ref/Bottom]), (RefLink[Left, paclet:ref/Left]), (RefLink[Right, paclet:ref/Right]), (RefLink[Center, paclet:ref/Center]).

By default, (RefLink[TreePlot, paclet:ref/TreePlot]) places each tree root at the top.

The following special wrappers can be used for the edges (Subscript[e, i]):

(RefLink[TreePlot, paclet:ref/TreePlot]) has the same options as (RefLink[Graphics, paclet:ref/Graphics]), with the following additions and changes:

Possible settings for (RefLink[PlotTheme, paclet:ref/PlotTheme]) include common base themes:

Graph feature themes affect the plots of vertices and edges. Feature themes include:

OpenersPrimaryExamplesSection(Spacer1)Examples  (37)

OpenersExampleSection0(Spacer1)Basic Examples  (5)

Plot a graph:

TreePlot[KaryTree[9,2]]

Plot a graph specified by edge rules:

TreePlot[{1->2,2->3,3->4,4->0,5->1,6->2,7->3,8->4,9->0}]

Plot a graph specified by its adjacency matrix:

TreePlot[{{0,1,1,1,0},{1,0,0,0,1},{1,0,0,0,0},{1,0,0,0,0},{0,1,0,0,0}}]

Drawing a tree with different orientation from the default:

TreePlot[{1->4,1->6,1->8,2->6,{3->8,"3->8"},4->5,7->8},Left]

Specify the root node:

Table[TreePlot[{1->4,1->6,1->8,2->6,3->8,4->5,7->8},Automatic,root,VertexLabels->Automatic],{root,{1,5}}]

OpenersExampleSection1(Spacer1)Scope  (10)

OpenersExampleSubsection0(Spacer1)Graph Specification  (4)

Specify a graph using a graph:

TreePlot[Graph[{1\[UndirectedEdge]4,1\[UndirectedEdge]7,2\[UndirectedEdge]5,3\[UndirectedEdge]4,4\[UndirectedEdge]5,5\[UndirectedEdge]6}]]

Specify a graph using a rule list:

TreePlot[{1->4,1->7,2->5,3->4,4->5,5->6}]

Specify a graph using a dense adjacency matrix:

TreePlot[{{0,1,0,0},{0,0,1,1},{0,0,0,0},{0,0,0,0}}]

Specify a graph using a sparse adjacency matrix:

TreePlot[SparseArray[{{1,2},{2,3},{2,4}}->1,{4,4}]]

OpenersExampleSubsection1(Spacer1)Graph Styling  (6)

Give labels for some edges:

TreePlot[{1->2,3->1,Labeled[2->4,"2->4"],2->5}]

Give vertex labels:

TreePlot[{1->2,3->1,2->4,2->5,1->2,5->5,3->6},VertexLabels->Automatic]

Show edges as arrows:

TreePlot[{1->3,1->5,2->6,2->7,2->8,4->6,5->6},DirectedEdges->True]

Plot a disconnected graph using different packing methods:

Table[Framed@TreePlot[Table[i->Mod[i^2,31],{i,0,31}],GraphLayout->{"PackingLayout"->pm}],{pm,{Automatic,"ClosestPacking"}}]

For large graphs, it is sometimes better not to draw vertices at all:

TreePlot[Table[i->Mod[i^2,351],{i,351}],VertexShapeFunction->None]

Drawing a tree with different orientations:

Grid[Partition[Table[TreePlot[{1->4,1->6,1->8,2->6,{3->8,"3->8"},4->5,7->8},pos,BaselinePosition->Top,PlotLabel->pos],{pos,{Automatic,Left,Top,Right,Bottom,Center}}],{3}]]

OpenersExampleSection2(Spacer1)Options  (7)

OpenersExampleSubsection2(Spacer1)DataRange  (1)

Specify the range of vertex coordinates:

TreePlot[{1->6,2-> 6,3-> 6,4-> 6,5-> 6}, DataRange->{{0,1},{0,1}}]

OpenersExampleSubsection3(Spacer1)DirectedEdges  (1)

Show directed edges:

TreePlot[{1->5,2->3,2->4,3->7,5->6,6->7,6->8},DirectedEdges-> True]

OpenersExampleSubsection4(Spacer1)LayerSizeFunction  (2)

Draw a tree with the first level of height 1, the second level 2, etc.:

TreePlot[{1->2,1->3,2->4,2->5,3->6,3->7},LayerSizeFunction->(#&)]

Plot a binary tree with random layer size:

TreePlot[Flatten[Table[{i-> 2i+j-1},{j,2},{i,2^7-1}]],Center,LayerSizeFunction->(RandomReal[{-4,4}]&)]

OpenersExampleSubsection5(Spacer1)PlotStyle  (3)

Specify an overall style for the graph:

Table[TreePlot[{1->3,1->5,1->6,1->7,2->3,4->5,5->8},PlotStyle->ps],{ps,{Red,Dashed,Directive[Red,Dashed]}}]

(RefLink[PlotStyle, paclet:ref/PlotStyle]) can be combined with (RefLink[VertexShapeFunction, paclet:ref/VertexShapeFunction]), which has higher priority:

TreePlot[{1->3,1->5,1->6,1->7,2->3,4->5,5->8},PlotStyle->Directive[PointSize[Large],Red],VertexShapeFunction->Function[{p,l},{Green,Point[p]}]]

(RefLink[PlotStyle, paclet:ref/PlotStyle]) can be combined with (RefLink[EdgeShapeFunction, paclet:ref/EdgeShapeFunction]), which has higher priority:

TreePlot[{1->3,1->5,1->6,1->7,2->3,4->5,5->8},PlotStyle->Directive[Dashed,Red],EdgeShapeFunction->Function[{p,vl},{Green,Line[p]}]]

OpenersExampleSection3(Spacer1)Applications  (8)

Define a complete binary tree of depth 3:

t=Flatten[Table[{i-> 2i+j-1},{j,2},{i,7}]]

Use different tree layouts:

Table[TreePlot[t,p],{p,{Top,Left,Bottom,Right,Center}}]

Draw a k-ary tree:

TreePlot[KaryTree[40,3],Center]

Generate and plot a random tree:

TreePlot[RandomInteger[#] -> # + 1 & /@ Range[0, 30]]

Delete random connections in a binary tree:

TreePlot[Delete[Flatten[Table[{i-> 2i+j-1},{j,2},{i,100}]],List/@RandomInteger[{1,100},{40}]]]

Link a number with one that is rotated one bit right:

TreePlot[Table[j->FromDigits[RotateRight[IntegerDigits[j,2]],2],{j,0,2^8-1}],Center]

Link a number with one that is rotated one bit left:

TreePlot[Table[j->FromDigits[RotateLeft[IntegerDigits[j,2]],2],{j,0,2^6-1}]]

Link a number with itself but with the last bit dropped:

TreePlot[Table[j->FromDigits[Drop[IntegerDigits[j,2],-1],2],{j,0,2^8-1}],Center]

Link a number with itself but with the first bit dropped:

TreePlot[Table[j->FromDigits[Drop[IntegerDigits[j,2],1],2],{j,0,2^6-1}]]

OpenersExampleSection4(Spacer1)Properties & Relations  (4)

If the graph is not a tree, it is laid out based on a spanning tree of this graph:

TreePlot[{1->2,2->3,3->4,4->1,3->5,1->2,5->5}]

Use (RefLink[LayeredGraphPlot, paclet:ref/LayeredGraphPlot]) for hierarchical-style drawing of a directed graph:

Table[pt[{1->4,1->6,1->8,2->6,3->8,4->5,7->8},PlotLabel->pt,DirectedEdges->True,PlotTheme->"DiagramBlue"],{pt,{TreePlot,LayeredGraphPlot}}]

Use (RefLink[GraphPlot, paclet:ref/GraphPlot]) or (RefLink[GraphPlot3D, paclet:ref/GraphPlot3D]) for general undirected graph drawing:

g={1->2,1->6,1->20,2->3,2->17,3->4,3->12,4->5,4->15,5->6,5->10,6->7,7->8,7->16,8->9,8->19,9->10,9->14,10->11,11->12,11->20,12->13,13->14,13->18,14->15,15->16,16->17,17->18,18->19,19->20};

{GraphPlot[g],GraphPlot3D[g]}

Use (RefLink[ArrayPlot, paclet:ref/ArrayPlot]) or (RefLink[MatrixPlot, paclet:ref/MatrixPlot]) to display sparse matrices:

g=ExampleData[{"Matrix","Bai/bfwa62"},"Matrix"]

{ArrayPlot[g],MatrixPlot[g]}

OpenersExampleSection5(Spacer1)Possible Issues  (2)

For nontree graphs, edges may overlap:

TreePlot[{1->2,1->3,1->4,2->3,2->4,3->4}, VertexLabels->Automatic]

Use (RefLink[LayeredGraphPlot, paclet:ref/LayeredGraphPlot]) or (RefLink[GraphPlot, paclet:ref/GraphPlot]) to avoid overlapping edges:

LayeredGraphPlot[{1->2,1->3,1->4,2->3,2->4,3->4},VertexLabels->Automatic]

GraphPlot[{1->2,1->3,1->4,2->3,2->4,3->4},VertexLabels->Automatic]

(RefLink[TreePlot, paclet:ref/TreePlot]) automatically chooses the base node to minimize tree height:

TreePlot[{1->2,2->3},PlotTheme->"DiagramGreen"]

Explicitly specify a top node, which in this case is also the root node:

TreePlot[{1->2,2->3},Top,1,PlotTheme->"DiagramGreen"]

OpenersExampleSection6(Spacer1)Neat Examples  (1)

Complete binary trees with random layer layout sizes:

Table[TreePlot[Flatten[Table[{i-> 2i+j-1},{j,2},{i,2^7-1}]],Center,LayerSizeFunction->(RandomReal[{-4,4}]&)],{6}]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[TreeGraph, paclet:ref/TreeGraph])  ▪  (RefLink[Graph, paclet:ref/Graph])  ▪  (RefLink[LayeredGraphPlot, paclet:ref/LayeredGraphPlot])  ▪  (RefLink[GraphPlot, paclet:ref/GraphPlot])  ▪  (RefLink[TreeForm, paclet:ref/TreeForm])  ▪  (RefLink[CommunityGraphPlot, paclet:ref/CommunityGraphPlot])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Data Visualization, paclet:guide/DataVisualization])}, {▪(RefLinkPlain[Computational Systems, paclet:guide/ComputationalSystemsAndDiscovery])}, {▪(RefLinkPlain[Scientific Data Analysis, paclet:guide/ScientificDataAnalysis])}, {▪(RefLinkPlain[Graph Visualization, paclet:guide/GraphVisualization])}, {▪(RefLinkPlain[Life Sciences & Medicine: Data & Computation, paclet:guide/LifeSciencesAndMedicineDataAndComputation])}, {▪(RefLinkPlain[Social Network Analysis, paclet:guide/SocialNetworks])}})}})