Changing the position of coordinate points randomly in a graph using python

Question:

I have a dataframe of 16 coordinate points.

import pandas as pd
import matplotlib.pyplot as plt
data = {'x': [-0.3162277660168379967, -0.3162277660168379967, -0.9486832980505139901, 0.3162277660168379967, 0.9486832980505139901, -0.3162277660168379967, -0.3162277660168379967, -0.9486832980505139901, 0.9486832980505139901, 0.3162277660168379967, 0.3162277660168379967, 0.3162277660168379967, 0.9486832980505139901, -0.9486832980505139901, -0.9486832980505139901, 0.9486832980505139901],
        'y': [-0.9486832980505139901, 0.3162277660168379967, 0.9486832980505139901, 0.3162277660168379967, -0.3162277660168379967, 0.9486832980505139901, -0.3162277660168379967, -0.3162277660168379967, 0.3162277660168379967, -0.9486832980505139901, -0.3162277660168379967, 0.9486832980505139901, -0.9486832980505139901, 0.3162277660168379967, -0.9486832980505139901, 0.9486832980505139901]
        }
df = pd.DataFrame(data)
df.plot(x='x', y='y', kind='scatter')
plt.show()

Now I want to randomly move their position from the original points (considered as noise in case of constellation diagram in wireless communications). Some of the new positions should be close to the their original one and rest of them should be in between two original points.
Adding or subtracting to the original points only shift to specific direction not in random direction. How can I do that?

Asked By: Rupak

||

Answers:

This may not be exactly what you’re hoping to do, but I hope that this will get you started. I’m not using pandas below, but you easily could change the code below to do so.

import matplotlib.pyplot as plt
from random import uniform, seed

data = {"x": [1, 9, 5, 8, 2], "y": [5, 1, 5, 2, 7]}
lower, upper = 0.7, 1.3 # allows for data to be changed by -30% to 30%
seed(100) # makes output the same every time, can change value to any number

# make randomized data
randomized_data = {}
for variable, value_lst in data.items():
    new_lst = []
    for value in value_lst:
        new_random_value = uniform(lower, upper) * value
        new_lst.append(new_random_value)
    randomized_data[variable] = new_lst


# this for loop is not necessary, just to help clarify answer in output graph
for i in range(len(data["x"])):
    points_x_vals = data["x"][i], randomized_data["x"][i]
    points_y_vals = data["y"][i], randomized_data["y"][i]
    plt.plot(points_x_vals, points_y_vals, "black")

# actual plotting of data
plt.scatter(data["x"], data["y"], label="original data")
plt.scatter(randomized_data["x"], randomized_data["y"], label=""randomized" data")
plt.legend()
plt.show()
Answered By: TimH
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.