如何将抖动添加到具有 X 和 Y 值的散点图?

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

我创建了随机数据并尝试在散点图中添加抖动,但我不知道如何为 X 和 Y 值应用抖动?我有 X 和 Y 形式的数据,但不是全部作为数据传递给 seaborn 绘图库。

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()

输出:

预期输出

谁能帮我解决这个问题?

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

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

结果: