Search code examples
pythonpython-3.xmatplotliblinear-regressionjitter

How to add Jitter to scatter plot with X and Y values?


I have created random data and trying to add jitter in scatter plot but I can't figure out how to apply jitter for the X and Y values? I have data in the form of X and Y but not the whole as a data to pass it to the seaborn plotting library.

def make_cubic_dataset(m, a=-3.0, b=1.0, c=3.5, d=4, mu=0.0, sigma=0.33):

    x = np.random.uniform(low=-1.0, high=1.0, size=(m,))   
    y =  a*x**3 + b*x**2 + c*x + d + np.random.normal(mu,sigma)
    #generates a random number from the normal distribution with mu and sigma.
    return (x,y)

np.random.seed(42)
x,y = make_cubic_dataset(100)

print(x.shape)
print(y.shape)
print(x[:5])
print(y[:5])

plt.scatter(x, y)
plt.title("Random Artificial Cubic dataset")
plt.xlabel("x")
plt.ylabel("y")
plt.show()

Output:

enter image description here

Expected output

enter image description here

Can anyone help me with this?


Solution

  • You're adding a single scalar random amount to your entire y variable, rather than an array of randomly distributed numbers. The following will produce a normally distributed array of random numbers with a standard deviation sigma:

    y =  a*x**3 + b*x**2 + c*x + d + np.random.randn(m)*sigma
    

    Result:

    enter image description here