Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

visualize a two-dimensional point set using Python

I'm new to Python and want to perform a rather simple task. I've got a two-dimensional point set, which is stored as binary data (i.e. (x, y)-coordinates) in a file, which I want to visualize. The output should look as in the picture below.

However, I'm somehow overwhelmed by the amount of google results on this topic. And many of them seem to be for three-dimensional point cloud visualization and/or a massive amount of data points. So, if anyone could point me to a suitable solution for my problem, I would be really thankful.

pointset

EDIT: The point set is contained in a file which is formatted as follows:

0.000000000000000   0.000000000000000
1.000000000000000   1.000000000000000
1
0.020375738732779   0.026169010160356
0.050815740313746   0.023209931647163
0.072530406907906   0.023975230642589

The first data vector is the one in the line below the single "1"; i.e. (0.020375738732779, 0.026169010160356). How do I read this into a vector in python? I can open the file using f = open("pointset file")

like image 521
0xbadf00d Avatar asked Sep 03 '25 06:09

0xbadf00d


1 Answers

Install and import matplotlib and pyplot:

import matplotlib.pyplot as plt

Assuming this is your data:

x = [1, 2, 5, 1, 5, 7, 8, 3, 2, 6]
y = [6, 7, 1, 2, 6, 2, 1, 6, 3, 1]

If you need, you can use a comprehension to split the coordinates into seperate lists:

x = [p[0] for p in points]
y = [p[1] for p in points]

Plotting is as simple as:

plt.scatter(x=x, y=y)

Result:

Scatter Plot

Many customizations are possible.

EDIT: following question edit

In order to read the file:

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]))
  
like image 168
Omri Avatar answered Sep 04 '25 19:09

Omri