繁体   English   中英

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

[英]Display displot in separate subplots - Seaborn

我的目标是将 Seaborn 显示插入单独的子图中。 使用下面,我想将Label1传递给ax1并将Label2传递给df2 但是,我收到警告: UserWarning: displot is a figure-level function and does not accept the ax= paramter. You may wish to try histplot. warnings.warn(msg, UserWarning) UserWarning: displot is a figure-level function and does not accept the ax= paramter. You may wish to try 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,
)

用 2 列显示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM