使用 Python 可视化二维点集

0xb*_*00d 4 python visualization visual-studio

我是 Python 新手,想要执行一项相当简单的任务。我有一个二维点集,它作为二进制数据(即(x, y)坐标)存储在文件中,我想将其可视化。输出应如下图所示。

然而,我对这个主题的谷歌结果的数量感到不知所措。其中许多似乎用于三维点云可视化和/或大量数据点。因此,如果有人能为我的问题指出一个合适的解决方案,我将非常感激。

点集

编辑:点集包含在格式如下的文件中:

0.000000000000000   0.000000000000000
1.000000000000000   1.000000000000000
1
0.020375738732779   0.026169010160356
0.050815740313746   0.023209931647163
0.072530406907906   0.023975230642589
Run Code Online (Sandbox Code Playgroud)

第一个数据向量是单个“1”下方行中的数据向量;IE (0.020375738732779, 0.026169010160356)。如何将其读入 python 中的向量?我可以使用打开该文件f = open("pointset file")

Omr*_*mri 7

安装并导入 matplotlib 和 pyplot:

import matplotlib.pyplot as plt
Run Code Online (Sandbox Code Playgroud)

假设这是您的数据:

x = [1, 2, 5, 1, 5, 7, 8, 3, 2, 6]
y = [6, 7, 1, 2, 6, 2, 1, 6, 3, 1]
Run Code Online (Sandbox Code Playgroud)

如果需要,您可以使用推导式将坐标拆分为单独的列表:

x = [p[0] for p in points]
y = [p[1] for p in points]
Run Code Online (Sandbox Code Playgroud)

绘图很简单:

plt.scatter(x=x, y=y)
Run Code Online (Sandbox Code Playgroud)

结果:

散点图

许多定制都是可能的。

编辑:以下问题编辑

为了读取文件:

x = []
y = []
with open('pointset_file.txt', 'r') as f:
    for line in f:
        coords = line.split(' ')
        x.append(float(coords[0]))
        y.append(float(coords[1]))
  
Run Code Online (Sandbox Code Playgroud)

  • 这种差异与列表指针有关。`x=y=[]` 仅在内存中创建两个变量引用的一个列表。附加到“x”,然后从“y”读取将在内存中显示相同的变化。许多人将 Jupyter 与 matplotlib 一起使用,因此他们不会处理窗口立即关闭的情况,但您应该在程序末尾添加一行“input()”,以便在窗口打开时暂停。如果欧米的回答对您有帮助,请采纳。 (2认同)