gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:30:52 25 4
gpt4 key购买 nike

如何缩放seaborn jointplot的边际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()

enter image description here

最佳答案

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

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()

sns.jointplot without common norm

关于matplotlib - 如何使用不平衡的分类数据缩放seaborn联合图的边际kdeplot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73345895/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com