如何使用Matplotlib制作具有光晕效果的散点图?

时间:2018-05-06 06:33:23

标签: python matplotlib plot

我想绘制一个高斯随机变量的潜在空间 其中x_i~Gaussian(u_i, simga_i) 我想像这张照片一样代表情节中的不确定性 plot made by someone 但我找不到任何关于如何使用Matplotlib执行此操作的API!

2 个答案:

答案 0 :(得分:0)

也许您可以尝试类似下面示例的内容? (从here修改)

import numpy as np
import matplotlib.pyplot as plt

def normal_pdf(x, mean, var):
    return np.exp(-(x - mean)**2 / (2*var))

xmin, xmax, ymin, ymax = (0, 100, 0, 100)

N = 1000
xx = np.linspace(xmin, xmax, N)
yy = np.linspace(ymin, ymax, N)

x = normal_pdf(xx, 50, 75)
y = normal_pdf(yy, 50, 75)

weights = np.array(np.meshgrid(x, y)).prod(0)

plt.imshow(weights, extent=(xmin, xmax, ymin, ymax), cmap=plt.cm.Blues, alpha=0.5)
plt.plot(50, 50, "bo")
plt.tight_layout()
plt.show()

结果:

enter image description here

答案 1 :(得分:0)

要添加更多点:

import numpy as np
import matplotlib.pyplot as plt

def normal_pdf(x, mean, var):
    return np.exp(-(x - mean)**2 / (2*var))

def add_point(mu, std, ax):
    x = normal_pdf(xx, mu, std)
    y = normal_pdf(yy, xmax-mu, std)
    weights = np.array(np.meshgrid(x, y)).prod(0)
    ax.imshow(weights, extent=(xmin, xmax, ymin, ymax), cmap=plt.cm.Blues, alpha=0.5)
    plt.plot(mu, mu, "bo")


xmin, xmax, ymin, ymax = (0, 150, 0, 150)

N = 1000
xx = np.linspace(xmin, xmax, N)
yy = np.linspace(ymin, ymax, N)

fig, _ax = plt.subplots()

add_point(50, 75, _ax)
add_point(10, 25, _ax)

plt.tight_layout()
plt.show()