如何使用不平衡的分类数据缩放seaborn联合图的边际kdeplot

Hyu*_*Kim 2 scaling matplotlib kernel-density seaborn jointplot

如何缩放seaborn联合图的边际kdeplot?

假设我们有 1000 个类型“a”的数据、100 个类型“b”的数据和“100”个类型“c”的数据。

在这种情况下,边际 kdeplot 的尺度看起来并不相同,因为分类数据的大小完全不同。

我如何使这些相同?

我制作了一个玩具脚本,如下所示:

import seaborn as sns
import numpy as np
import pandas as pd
import matplotlib.pylab as plt

ax, ay = 1 * np.random.randn(1000) + 2, 1 * np.random.randn(1000) + 2
bx, by = 1 * np.random.randn(100) + 3, 1 * np.random.randn(100) + 3
cx, cy = 1 * np.random.randn(100) + 4, 1 * np.random.randn(100) + 4

a = [{'x': x, 'y': y, 'kind': 'a'} for x, y in zip(ax, ay)]
b = [{'x': x, 'y': y, 'kind': 'b'} for x, y in zip(bx, by)]
c = [{'x': x, 'y': y, 'kind': 'c'} for x, y in zip(cx, cy)]

df = pd.concat([pd.DataFrame.from_dict(a), pd.DataFrame.from_dict(b), pd.DataFrame.from_dict(c)], ignore_index=True)

print(df)
             x         y kind
0     2.500866  2.700925    a
1    -0.386057  3.322318    a
2     1.691078  2.558366    a
3     2.235042 -0.113836    a
4     3.331039  1.138366    a
...        ...       ...  ...
1195  3.703245  2.935332    c
1196  1.806040  2.842754    c
1197  5.431313  5.377297    c
1198  3.873162  6.200356    c
1199  4.111234  3.038126    c

[1200 rows x 3 columns]

sns.jointplot(data=df, x='x', y='y', hue="kind")
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Joh*_*anC 5

您可以使用marginal_kws=为边缘图添加关键字。在这种情况下,边际使用sns.kdeplot具有诸如commmon_norm和 等参数的multiple

import seaborn as sns
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

ax, ay = 1 * np.random.randn(1000) + 2, 1 * np.random.randn(1000) + 2
bx, by = 1 * np.random.randn(100) + 3, 1 * np.random.randn(100) + 3
cx, cy = 1 * np.random.randn(100) + 4, 1 * np.random.randn(100) + 4

a = [{'x': x, 'y': y, 'kind': 'a'} for x, y in zip(ax, ay)]
b = [{'x': x, 'y': y, 'kind': 'b'} for x, y in zip(bx, by)]
c = [{'x': x, 'y': y, 'kind': 'c'} for x, y in zip(cx, cy)]

df = pd.concat([pd.DataFrame.from_dict(a), pd.DataFrame.from_dict(b), pd.DataFrame.from_dict(c)], ignore_index=True)

sns.jointplot(data=df, x='x', y='y', hue="kind" , marginal_kws={'common_norm':False})
plt.show()
Run Code Online (Sandbox Code Playgroud)

sns.jointplot 没有通用规范