【发布时间】:2015-10-25 22:01:43
【问题描述】:
我想使用 matplotlib 绘制元组列表的散点图,其元素是 x 和 y 坐标。它们的连接性由另一个列表确定,该列表说明哪个点连接到哪个点。我到目前为止是这样的:
import itertools
import matplotlib.pyplot as plt
coords = [(0.0, 0.0), (1.0, 1.0), (1.0, 0.0), (2.0, 1.0), (2.0, 0.0), (3.0, 1.0)]
connectivity = coords[0] <--> coords[1], coords[2]
coords[1] <--> coords[0], coords[2], coords[3]
coords[2] <--> coords[0], coords[1], coords[4]
coords[3] <--> coords[1], coords[3], coords[5]
coords[4] <--> coords[2], coords[3], coords[5]
coords[5] <--> coords[3], coords[4]
x, y = zip(*coords)
plt.plot(x, y, '-o')
plt.show()
我知道连接部分不是真正的 python 脚本。我把它包括在内是为了向大家展示这些点应该如何连接。运行此脚本时(没有连接位),我得到下图:
但是,我希望情节显示为:
有什么想法可以做到这一点吗?
【问题讨论】:
-
您的第二行有 8 个段,而第一行有 5 个。这是一个不同的数字,因此您需要一个不同的列表,或者使用您的点序列中的函数重建正确的列表。它也必须分两步进行,因为该图不能绘制为单条连续线。
标签: python matplotlib