2

I'm seeking ideas to plot a tuple tree t = ((4,), (3, 5,), (2, 4, 6,), (1, 3, 5, 7,)) as the following image (assuming this binomial tree size can change). I'm trying to avoid dependencies on non-core packages (just sticking to pandas, numpy, matplotlib, scikit, and such).

enter image description here

BenBarnes
  • 19,114
  • 6
  • 56
  • 74
Oleg Melnikov
  • 3,080
  • 3
  • 34
  • 65
  • Have you looked at this http://stackoverflow.com/questions/7670280/tree-plotting-in-python – Leb Nov 14 '15 at 19:26
  • @Leb : Interesting post, but it refers to `graphviz` package or software. I just need a simple way of plotting a tree so as to add it to a package without any extra dependencies. Still, thanks for the reference. – Oleg Melnikov Nov 14 '15 at 20:36

1 Answers1

2

I'm using this piece of code which gives a pretty good result:

from matplotlib import pyplot as plt
import numpy as np

fig = plt.figure(figsize=[5, 5])
for i in range(3):
    x = [1, 0, 1]
    for j in range(i):
        x.append(0)
        x.append(1)
    x = np.array(x) + i
    y = np.arange(-(i+1), i+2)[::-1]
    plt.plot(x, y, 'bo-')
plt.show()

enter image description here

Anton Menshov
  • 2,266
  • 14
  • 34
  • 55
Fruit
  • 66
  • 8