在Python中绘制树的边缘

时间:2015-11-15 13:38:07

标签: python python-3.x matplotlib graphics plot

以[(x1,y1),(x2,y2)]形式绘制坐标列表的有效方法是什么,其中每对坐标由一条线连接(如下所示)。我想避免使用非核心软件包(核心是pandas,matplotlib,numpy,......)

示例:

c = [[(0, 4), (1, 3)],
     [(0, 4), (1, 5)],
     [(1, 3), (2, 2)],
     [(1, 3), (2, 4)],
     [(1, 5), (2, 4)],
     [(1, 5), (2, 6)],
     [(2, 2), (3, 1)],
     [(2, 2), (3, 3)],
     [(2, 4), (3, 3)],
     [(2, 4), (3, 5)],
     [(2, 6), (3, 5)],
     [(2, 6), (3, 7)]]

绘制为(带标签):

enter image description here

1 个答案:

答案 0 :(得分:2)

就有效方式而言,'我不确定。以下是我认为可以调整/调整的工作方式更有效'

import matplotlib.pyplot as plt

c = [[(0, 4), (1, 3)],
     [(0, 4), (1, 5)],
     [(1, 3), (2, 2)],
     [(1, 3), (2, 4)],
     [(1, 5), (2, 4)],  # I changed this to match your plot
     [(1, 5), (2, 6)],
     [(2, 2), (3, 1)],
     [(2, 2), (3, 3)],
     [(2, 4), (3, 3)],
     [(2, 4), (3, 5)],
     [(2, 6), (3, 5)],
     [(2, 6), (3, 7)]]

fig = plt.figure()
ax = fig.add_subplot(111)

annotated = set()
for l in c:
    d = [[p[0] for p in l], [p[1] for p in l]]
    ax.plot(d[0], d[1], 'k-*')
    for p in l:
        annotated.add(p)

for p in annotated:
    ax.annotate(str(p), xy=p)

plt.xlim([0, 3.5])
plt.ylim([0, 8])
plt.show()

enter image description here