簡體   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