首页 > 解决方案 > 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?

标签: pythonpython-3.xmatplotliblinear-regressionjitter

解决方案


您将单个标量随机量添加到整个 y 变量,而不是随机分布的数字数组。以下将产生一个具有标准偏差的正态分布随机数数组sigma

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

结果:

在此处输入图像描述


推荐阅读