python - 绘制给定点集的轮廓

标签 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) 来计算。

最佳答案

您可以将数据插值到二维网格上。有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

关于python - 绘制给定点集的轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33704428/

相关文章:

删除 ggplot2 中的负绘图区域

r - 设置单个数据点的颜色

python - 为什么球的行为方式是这样的?

python - 整合 Pandas 数据帧 : Choose the smaller absolute value

python - 我正在尝试从文本中获取二进制数据并转换为 numpy 数组

python - 如何在 matplotlib 箱线图中标记四分位数?

python - 如何将 matplotlib 轴刻度标签 fontweight 设置为粗体?

python - 使用 Matplotlib 绘制正态分布

iphone - Python 到 Objective C(预期学习曲线)

python - Pandas .str.replace 和不区分大小写