gpt4 book ai didi

python - 在单独的子图中显示 displot - Seaborn

转载 作者:行者123 更新时间:2023-12-04 03:34:02 29 4
gpt4 key购买 nike

我的目标是将 Seaborn 显示插入单独的子图中。使用下面,我想将 Label1 传递给 ax1 并将 Label2 传递给 df2。但是,我收到警告:UserWarning: displot is a figure-level function and does not accept the ax= paramter.你不妨试试 histplot。 warnings.warn(msg, UserWarning)

有解决办法吗?关于 hue 参数,我之前遇到过 histplot 的问题。如果有必要,我不愿意使用它。

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

fig, (ax1, ax2) = plt.subplots(1,2, figsize = (12,6))

df = pd.DataFrame({
'Num' : [1,2,1,2,3,2,1,3,2,2,1,2,3,3,1,3],
'Label1' : ['A','B','C','B','B','C','C','B','B','A','C','A','B','A','C','A'],
'Label2' : ['D','E','D','F','E','D','F','E','F','D','E','F','E','D','D','F'],
'Item' : ['Up','Left','Up','Left','Down','Right','Up','Down','Right','Down','Right','Up','Up','Right','Down','Left'],
})

ax1 = sns.displot(data = df,
x = 'Label1',
hue = 'Num',
row = 'Item',
row_order = ['Up','Down','Left','Right'],
shrink = 0.9,
discrete = True,
aspect = 4,
height = 2,
ax = ax1
)

ax2 = sns.displot(data = df,
x = 'Label2',
hue = 'Num',
row = 'Item',
row_order = ['Up','Down','Left','Right'],
shrink = 0.9,
discrete = True,
aspect = 4,
height = 2,
ax = ax2
)

最佳答案

如果你想避免 histplot(),我认为你可以用 displot() 得到的最接近的是 melt()数据并使用 colLabel1Label2 分成两列:

sns.displot(
data = df.melt(['Num', 'Item'], var_name='Group', value_name='Label'),
y = 'Label',
hue = 'Num',
row = 'Item',
col = 'Group',
row_order = ['Up', 'Down', 'Left', 'Right'],
shrink = 0.9,
discrete = True,
aspect = 4,
height = 2,
)

displot with 2 cols

关于python - 在单独的子图中显示 displot - Seaborn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67294076/

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