绘制给定点集的轮廓

时间:2015-11-14 02:11:36

标签: python matplotlib plot seaborn contourf

我有一组给定的点(x,y,F(x,y)),我想绘制一个轮廓图,其中(x,y)显示为点,轮廓计算为水平曲线F(X,Y)。有谁知道如何用seaborn做到这一点?

我希望像sepal_width与sepal_length图一样看起来像http://goo.gl/SWThWS(没有边缘),除了核心密度估计不应该使用点的空间密度计算,而是F(x, y)相反。

1 个答案:

答案 0 :(得分:1)

您可以将数据插入到2D网格中。有lots of ways to do this - 可能与核密度估计最接近的类比是使用radial basis function进行插值:

import numpy as np
from scipy.interpolate import Rbf
from matplotlib import pyplot as plt

def f(x, y):
    return np.sin(x) + np.cos(2 * y)

# 1D arrays of points
x = np.random.rand(100) * 2 * np.pi
y = np.random.rand(100) * 2 * np.pi
z = f(x, y)

# initialize radial basis function
rb = Rbf(x, y, z)

# interpolate onto a 100x100 regular grid
X, Y = np.mgrid[:2*np.pi:100j, :2*np.pi:100j]
Z = rb(X.ravel(), Y.ravel()).reshape(X.shape)

# plotting
fig, ax = plt.subplots(1, 1)
ax.set_aspect('equal')
ax.hold(True)
m = ax.contourf(X, Y, Z, 20, cmap=plt.cm.Greens)
ax.scatter(x, y, c=z, s=60, cmap=m.cmap, vmin=m.vmin, vmax=m.vmax)
cb = fig.colorbar(m)
cb.set_label('$f(x, y)$', fontsize='xx-large')
ax.set_xlabel('$x$', fontsize='xx-large')
ax.set_ylabel('$y$', fontsize='xx-large')
ax.margins(0.05)
fig.tight_layout()
plt.show()

enter image description here