从seaborn kdeplots中提取特征参数
Extracting characteristic parameters from seaborn kdeplots
我希望能够从使用 Python 的 Seaborn 生成的内核密度图中提取特征参数。虽然在获得分布的中位数方面有一个非常好的 ,但我想看看这是否可以推广到 1D 数据的多峰分布,尤其是在 2D 情况下。
下面是一个最小的例子,我从中手动导出一维情况下每个峰的值。我希望利用可用的对象找到更系统和适用于 2D 的东西。
import numpy as np
import scipy
import pandas as pd
import seaborn as sns
sns.set(style="white", color_codes=True, font_scale=2)
x1 = np.random.normal(-1.5,1,1000)
y1 = np.random.normal(1.5,1,1000)
x2 = np.random.normal(1.5,1,1000)
y2 = np.random.normal(-1.5,1,1000)
x = np.concatenate((x1,x2))
y = np.concatenate((y1,y2))
d = {'x': pd.Series(x), 'y': pd.Series(y)}
data = pd.DataFrame(d)
px = sns.kdeplot(data.x, shade=True)
x,y = px.get_lines()[0].get_data()
xysel = np.array([(x,y) for x,y in zip(x,y) if x < 0])
imax = np.argmax(xysel[:,1])
x_median = xysel[imax,0]
y_median = xysel[imax,1]
plt.vlines(x_median, 0, y_median, linestyles='dashed', color='b')
px.set_xlim(-5,5)
plt.show()
py = sns.kdeplot(data.y, shade=True, color='r')
x,y = py.get_lines()[0].get_data()
xysel = np.array([(x,y) for x,y in zip(x,y) if x > 0])
imax = np.argmax(xysel[:,1])
x_median = xysel[imax,0]
y_median = xysel[imax,1]
plt.vlines(x_median, 0, y_median, linestyles='dashed', color='r')
py.set_xlim(-5,5)
plt.show()
p = sns.kdeplot(data.x, data.y, shade=True)
您可以通过以下代码获取路径:
ax = sns.kdeplot(data.x, data.y, shade=True)
for path in ax.collections[-1].get_paths():
x, y = path.vertices.mean(axis=0)
ax.plot(x, y, "ro")
这是输出:
ax.collections
是 PathCollection
个对象的列表,对应于 Axes 对象中的每个级别。
每个 PathCollection
包含一个 Path
对象的列表,您可以通过 get_paths()
方法获得这些对象。
路径的点保存在vertices
数组中。
如果想获取更多信息,需要获取Axes.contourf
的return对象,先修补contourf()
方法:
from matplotlib.axes import Axes
def contourf(self, *args, **kw):
self._quadcontourset = self.old_contourf(*args, **kw)
return self._quadcontourset
Axes.old_contourf = Axes.contourf
Axes.contourf = contourf
然后可以通过ax._quadcontourset
得到QuadContourSet
对象。请阅读 QuadContourSet
的源代码以了解如何使用它。
我希望能够从使用 Python 的 Seaborn 生成的内核密度图中提取特征参数。虽然在获得分布的中位数方面有一个非常好的
下面是一个最小的例子,我从中手动导出一维情况下每个峰的值。我希望利用可用的对象找到更系统和适用于 2D 的东西。
import numpy as np
import scipy
import pandas as pd
import seaborn as sns
sns.set(style="white", color_codes=True, font_scale=2)
x1 = np.random.normal(-1.5,1,1000)
y1 = np.random.normal(1.5,1,1000)
x2 = np.random.normal(1.5,1,1000)
y2 = np.random.normal(-1.5,1,1000)
x = np.concatenate((x1,x2))
y = np.concatenate((y1,y2))
d = {'x': pd.Series(x), 'y': pd.Series(y)}
data = pd.DataFrame(d)
px = sns.kdeplot(data.x, shade=True)
x,y = px.get_lines()[0].get_data()
xysel = np.array([(x,y) for x,y in zip(x,y) if x < 0])
imax = np.argmax(xysel[:,1])
x_median = xysel[imax,0]
y_median = xysel[imax,1]
plt.vlines(x_median, 0, y_median, linestyles='dashed', color='b')
px.set_xlim(-5,5)
plt.show()
py = sns.kdeplot(data.y, shade=True, color='r')
x,y = py.get_lines()[0].get_data()
xysel = np.array([(x,y) for x,y in zip(x,y) if x > 0])
imax = np.argmax(xysel[:,1])
x_median = xysel[imax,0]
y_median = xysel[imax,1]
plt.vlines(x_median, 0, y_median, linestyles='dashed', color='r')
py.set_xlim(-5,5)
plt.show()
p = sns.kdeplot(data.x, data.y, shade=True)
您可以通过以下代码获取路径:
ax = sns.kdeplot(data.x, data.y, shade=True)
for path in ax.collections[-1].get_paths():
x, y = path.vertices.mean(axis=0)
ax.plot(x, y, "ro")
这是输出:
ax.collections
是 PathCollection
个对象的列表,对应于 Axes 对象中的每个级别。
每个 PathCollection
包含一个 Path
对象的列表,您可以通过 get_paths()
方法获得这些对象。
路径的点保存在vertices
数组中。
如果想获取更多信息,需要获取Axes.contourf
的return对象,先修补contourf()
方法:
from matplotlib.axes import Axes
def contourf(self, *args, **kw):
self._quadcontourset = self.old_contourf(*args, **kw)
return self._quadcontourset
Axes.old_contourf = Axes.contourf
Axes.contourf = contourf
然后可以通过ax._quadcontourset
得到QuadContourSet
对象。请阅读 QuadContourSet
的源代码以了解如何使用它。